What is ArrayList in C#?
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#?
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); } } }
Output:
12.56 hello G 1234
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); } }
Output:
元素的初始数目 : 10 使用remove()方法之后,元素数: 9 使用removeat()方法之后,元素数: 8 使用removerange()方法之后,元素数:5 使用clear()方法之后,元素数: 0
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!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



1. The difference between Iterator and foreach is the polymorphic difference (the bottom layer of foreach is Iterator) Iterator is an interface type, it does not care about the type of collection or array; both for and foreach need to know the type of collection first, even the type of elements in the collection; 1. Why is it said that the bottom layer of foreach is the code written by Iterator: Decompiled code: 2. The difference between remove in foreach and iterator. First, look at the Alibaba Java Development Manual, but no error will be reported in case 1, and an error will be reported in case 2 (java. util.ConcurrentModificationException) first

You can use the contains() method of the List interface to check whether an object exists in the list. contains() method booleancontains(Objecto) Returns true if this list contains the specified element. More formally, returns true if and only if this list contains at least one element e such that (o==null?e==null:o.equals(e)). Parameter c - the element whose presence in this list is to be tested. Return Value Returns true if this list contains the specified element. Throws ClassCastException - if the specified element's type is incompatible with this list (optional). NullP

Use java's ArrayList.remove() function to remove elements from an ArrayList. In Java, ArrayList is a commonly used collection class used to store and operate a set of elements. The ArrayList class provides many methods to add, delete, modify, and query elements in the collection. One of the more frequently used methods is remove(), which can remove elements from an ArrayList. The remove() method of ArrayList has two overloaded forms: one

Why is the initial capacity of HashMap 16? When talking about the initialization capacity of ArrayList, we must first review the initialization capacity of HashMap. Taking the Java8 source code as an example, there are two relevant factors in HashMap: initialization capacity and loading factor: /***Thedefaultinitialcapacity-MUSTbeapoweroftwo.*/staticfinalintDEFAULT_INITIAL_CAPACITY=1>1);if(newCapacity-minCapacity0)newCapacity=hugeCapacity

Use Java's ArrayList.clear() function to clear the elements in the ArrayList. In Java programming, ArrayList is a very commonly used data structure that can dynamically store and access elements. However, in some cases, we may need to clear all elements in the ArrayList in order to reuse or free the memory. At this time, you can use the clear() function of ArrayList to achieve it. ArrayList.clear()

Java uses the contains() function of the ArrayList class to determine whether an element exists. ArrayList is a very commonly used data structure in Java programming. It provides a flexible way to store and manipulate a set of data. In addition to simply adding, deleting and accessing elements, ArrayList also provides some useful methods, such as the contains() function, which is used to determine whether an element exists in the ArrayList. contains() function is A

ArrayListisaclassofJavaCollectionFrameworkthatimplementsListInterface.Itisalinearstructurethatstoresandaccesseseachelementsequentially.Itallowsthestorageofduplicateelementshowever,thereareafewapproachesthatmayhelptogetuniquevaluesfromanArrayList.Inth

How to add elements in Java using the add() method of the ArrayList class. ArrayList is one of the common collection classes in Java. It provides convenient methods to manage dynamic length arrays. Adding elements to an ArrayList is one of the common operations, and the add() method is one of the main methods to implement this operation. The use of the add() method is very simple, it can add an element to the end of the ArrayList. Below is a sample code that demonstrates how to use a
