Home Backend Development C#.Net Tutorial C# ArrayListd length problem solution

C# ArrayListd length problem solution

Oct 08, 2017 am 09:35 AM
.net

C# Solving the length problem of ArrayListd

namespace ArrayListd的长度问题
{
   class Program
   {
       static void Main(string[] args)
       {  
           //需要的参数是object类型
           //alt+shift+F10添加引用using System.Collections;
           ArrayList list = new ArrayList();
           //count 表示集合中实际包含的元素个数
           //capity集合中可以包含的元素的个数
           //超过了包含的个数的时候,集合就会向内存中多申请开辟一倍的空间
           list.Add(2);
           list.Add(1);
           list.Add(2);
           list.Add(3);
           list.Add(4);
          // list.RemoveAt(0);//移除某个索引位置的元素
           list.Sort();//123456
          // list.Reverse();//654321
           list.TrimToSize();//如果加上这个,list.Capacity这个是实际的元素数,不是4,8,12了
            list.ToArray();
foreach (var item in list)
           {
               Console.WriteLine(item);
           }
// list.Clear();//经所有的元素清除完
            bool b=  list.Contains(1);//看看元素中是否包含某个元素  1
          Console.WriteLine(list.Count);//1-2
          Console.WriteLine(list.Capacity);//Capacity这个属性是,超过四个元素变成8,超过8变成12
          Console.WriteLine(b);
          Console.ReadKey();
}
   }
}
===================================================
namespace ArrayList练习
{
   class Program
   {
       static void Main(string[] args)
       {
         #region add.list()
//            //不是静态类,就可以创建一个对象
//            //集合:很多数据的集合
//            //集合的好处:长度任意改变,类型不固定
//            //数组的长度不可变,类型单一
//            ArrayList List = new ArrayList();
//            List.Add(0);//这个地方放什么都可以
//            List.Add(3.14);
//            List.Add("zhangsan ");
//            List.Add(true);
//            List.Add('c');

//            List.Add(new int[]{1,2,3,4,5});
//            Person p = new Person();
//            List.Add(p);//自定义类的对象放进去
//            //List.Add(list);
//            for (int i = 0; i < List.Count; i++)
//            {      //List[i]可以装换成person类型
//                if (List[i] is Person)
//                {
//                    //((Person)List[i]).say();
//                }
//                Console.WriteLine(List[i]);
//                else if (List[i] is int[])
//                {                       // 强装换成int[]类型
//                    for (int j = 0; j < ((int[])List[i]).Length; j++)
//                    {
//                        Console.WriteLine(((int[])List[i])[j]);
//                    }
//                }
//                else
//                {
//                    Console.WriteLine(List[i]);
//                }
//            }
//            Console.ReadKey();
#endregion
           ArrayList List = new ArrayList();
           //添加单个元素
           List.Add(1);
           List.Add(2);
           List.Add(6);
           List.Add(0);
          // List.Add("张三");
           //添加集合
           List.AddRange(new int[]{1,2,3,4,5,6,7});
           //记住在ArrayLi中List的长度是用Count基数的,不是Length
           //移除元素
           //List.Clear();//清空所有元素
           //List.Remove(1);//移除单个元素,括号里写谁就删除谁
           //List.RemoveAt(0);//根据下标来删除元素,这个1是下标1也就是zahngsan
          // List.RemoveRange(0,4);
           //还是根据下标开始删除括号里的意思是从第0个下标开始删除删除2个元素
           //后面是4,把前面的单个元素删除完毕后就开始删除数组里面的元素
           //List.Sort();//升续排序
           // List.Reverse();//反转
           //插入到要插入的元素后面,后面的插入的没有类型要求
           List.Insert(1, "我是插入的");
          //插入到指定位置索引
           List.InsertRange(1,new string[]{"李四,老五,赵六"});
           //判断是否包含某个指定的元素,用bool类型接收一下
           bool b = List.Contains("我是插入的");
            Console.WriteLine(b);
            if (!List.Contains("猪"))
            {
                List.Add("猪");
            }
            else
            {
                Console.WriteLine("ppp");
            }
for (int i = 0; i < List.Count; i++)
           {
               //输出也是输出每一个元素List[i]
               Console.WriteLine(List[i]);
           }
           Console.ReadKey();
       }


   }
   public class Person
   { 
    public static void say()
       {
           Console.WriteLine("我是人类");
       }
   }
}
Copy after login

The above is the detailed content of C# ArrayListd length problem solution. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
1 months ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

What are the employment prospects of C#? What are the employment prospects of C#? Oct 19, 2023 am 11:02 AM

Whether you are a beginner or an experienced professional, mastering C# will pave the way for your career.

Share several .NET open source AI and LLM related project frameworks Share several .NET open source AI and LLM related project frameworks May 06, 2024 pm 04:43 PM

The development of artificial intelligence (AI) technologies is in full swing today, and they have shown great potential and influence in various fields. Today Dayao will share with you 4 .NET open source AI model LLM related project frameworks, hoping to provide you with some reference. https://github.com/YSGStudyHards/DotNetGuide/blob/main/docs/DotNet/DotNetProjectPicks.mdSemanticKernelSemanticKernel is an open source software development kit (SDK) designed to integrate large language models (LLM) such as OpenAI, Azure

.NET performance optimization technology for developers .NET performance optimization technology for developers Sep 12, 2023 am 10:43 AM

If you are a .NET developer, you must be aware of the importance of optimizing functionality and performance in delivering high-quality software. By making expert use of the provided resources and reducing website load times, you not only create a pleasant experience for your users but also reduce infrastructure costs.

Performance differences between Java framework and .NET framework Performance differences between Java framework and .NET framework Jun 03, 2024 am 09:19 AM

In terms of high-concurrency request processing, .NETASP.NETCoreWebAPI performs better than JavaSpringMVC. The reasons include: AOT early compilation, which reduces startup time; more refined memory management, where developers are responsible for allocating and releasing object memory.

C# .NET Interview Questions & Answers: Level Up Your Expertise C# .NET Interview Questions & Answers: Level Up Your Expertise Apr 07, 2025 am 12:01 AM

C#.NET interview questions and answers include basic knowledge, core concepts, and advanced usage. 1) Basic knowledge: C# is an object-oriented language developed by Microsoft and is mainly used in the .NET framework. 2) Core concepts: Delegation and events allow dynamic binding methods, and LINQ provides powerful query functions. 3) Advanced usage: Asynchronous programming improves responsiveness, and expression trees are used for dynamic code construction.

Advanced C# .NET Tutorial: Ace Your Next Senior Developer Interview Advanced C# .NET Tutorial: Ace Your Next Senior Developer Interview Apr 08, 2025 am 12:06 AM

Interview with C# senior developer requires mastering core knowledge such as asynchronous programming, LINQ, and internal working principles of .NET frameworks. 1. Asynchronous programming simplifies operations through async and await to improve application responsiveness. 2.LINQ operates data in SQL style and pay attention to performance. 3. The CLR of the NET framework manages memory, and garbage collection needs to be used with caution.

C# .NET: Exploring Core Concepts and Programming Fundamentals C# .NET: Exploring Core Concepts and Programming Fundamentals Apr 10, 2025 am 09:32 AM

C# is a modern, object-oriented programming language developed by Microsoft and as part of the .NET framework. 1.C# supports object-oriented programming (OOP), including encapsulation, inheritance and polymorphism. 2. Asynchronous programming in C# is implemented through async and await keywords to improve application responsiveness. 3. Use LINQ to process data collections concisely. 4. Common errors include null reference exceptions and index out-of-range exceptions. Debugging skills include using a debugger and exception handling. 5. Performance optimization includes using StringBuilder and avoiding unnecessary packing and unboxing.

C# Code within .NET: Exploring the Programming Process C# Code within .NET: Exploring the Programming Process Apr 12, 2025 am 12:02 AM

The programming process of C# in .NET includes the following steps: 1) writing C# code, 2) compiling into an intermediate language (IL), and 3) executing by the .NET runtime (CLR). The advantages of C# in .NET are its modern syntax, powerful type system and tight integration with the .NET framework, suitable for various development scenarios from desktop applications to web services.

See all articles