What is ArrayList in C#?

青灯夜游
Release: 2019-04-18 14:29:02
Original
19230 people have browsed it

ArrayList is a powerful feature of the C# language. It is a collection of non-generic types defined in the System.Collections namespace. The following article will take you to understand ArrayList, I hope it will be helpful to you. [Video tutorial recommendation: C# tutorial]

What is ArrayList in C#?

What is ArrayList in C#?

ArrayList is a collection of non-generic types defined in the System.Collections namespace. It is used to create dynamic arrays meaning the size of the array automatically increases or decreases as per the requirement of the program, no need to specify the size of ArrayList. Or in other words, ArrayList represents an ordered collection of objects that can be indexed individually.

In ArrayList, we can store elements of the same type and different types. It is a non-generic collection.

Note:

ArrayList is defined under the System.Collections namespace; therefore, when using Arraylist in a program, the System.Collections namespace must be added.

How to create an ArrayList?

The ArrayList class has three constructors for creating ArrayList.

●ArrayList(): used to create an instance of the ArrayList class; the instance is empty and has no initial capacity.

●ArrayList (Int32): Used to create an instance of the ArrayList class; the instance is empty and has a specified initial capacity.

●ArrayList (ICollection): Used to create an array list that is initialized with elements from the specified collection and has the same initial capacity copied from the collection.

Let’s take an example to see how to use the ArrayList() constructor to create an arraylist:

Example: Create an ArrayList, add elements to the ArrayList and access the ArrayList Elements.

using System; 
using System.Collections; 
  
class hello{ 
  
    // Main方法
    static public void Main() 
    { 
  
        // 创建数组列表
        ArrayList arraylist = new ArrayList(); 
  
        //向arraylist中不同类型的元素
        arraylist.Add(12.56); 
        arraylist.Add("hello"); 
        arraylist.Add(null); 
        arraylist.Add('G'); 
        arraylist.Add(1234); 
  
        // 使用foreach循环访问arraylist数组列表的元素 
        foreach(var elements in arraylist) 
        { 
            Console.WriteLine(elements); 
        } 
    } 
}
Copy after login

Output:

12.56
hello

G
1234
Copy after login

Description: The above code

1. Use the using keyword and include the System.Collections namespace

2. Use the ArrayList class to create an ArrayList

3. Use the Add() method to add elements to the ArrayList

4. Use the foreach loop to access the elements of the ArrayList; except for the foreach loop , can also be accessed using a for loop or indexer.

How to delete elements from ArrayList?

In ArrayList, we can delete elements from ArrayList. It provides four different methods to remove elements, the methods are:

● Remove() method: used to remove the first matching item of a specific object from the ArrayList.

● RemoveAt() method: used to delete the element at the specified index of ArrayList.

● RemoveRange() method: used to remove a range of elements from ArrayList.

●Clear() method: used to delete all elements from ArrayList.

Let’s take an example to see how to delete elements from ArrayList.

Example:

using System; 
using System.Collections; 
  
class A { 
  
    static public void Main() 
    { 
  
        // 创建数组列表
        ArrayList arraylist = new ArrayList(); 
  
        // 在array 中添加相同类型元素
        arraylist.Add('q'); 
        arraylist.Add('w'); 
        arraylist.Add('e'); 
        arraylist.Add('r'); 
        arraylist.Add('t'); 
        arraylist.Add('y'); 
        arraylist.Add('u'); 
        arraylist.Add('i'); 
        arraylist.Add('o'); 
        arraylist.Add('p'); 
  
        Console.WriteLine("元素的初始数目 : " 
                                       + arraylist.Count); 
  
        // 使用remove()方法从arraylist中移除“t”元素
        arraylist.Remove('t'); 
        Console.WriteLine("使用remove()方法之后,元素数: " + arraylist.Count); 
  
        // 使用removeat()方法删除索引8中的元素
        arraylist.RemoveAt(8); 
        Console.WriteLine("使用removeat()方法之后,元素数: " + arraylist.Count); 
  
        // 使用removerange()方法移除从索引1开始的3个元素
        arraylist.RemoveRange(1, 3); 
        Console.WriteLine("使用removerange()方法之后,元素数:" + arraylist.Count); 
  
        // 使用clear()方法删除arraylist中的所有元素
        arraylist.Clear(); 
        Console.WriteLine("使用clear()方法之后,元素数: " + arraylist.Count); 
    } 
}
Copy after login

Output:

元素的初始数目 : 10
使用remove()方法之后,元素数: 9
使用removeat()方法之后,元素数: 8
使用removerange()方法之后,元素数:5
使用clear()方法之后,元素数: 0
Copy after login

The above is the entire content of this article, I hope it will be helpful to everyone's learning. For more exciting content, you can pay attention to the relevant tutorial columns of the PHP Chinese website! ! !

The above is the detailed content of What is ArrayList in C#?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template