- Collection
- A collection is a container in which you use collective management-related groups of objects
- Collections are divided into non-generic collections and generic collections
- Non-generic collection
- Using a non-generic collection requires the introduction of a namespace System.Collections
- arraylist-arrays that can be dynamically added as needed
- hashtable-hash table used to store key-value pairs
- Queue-follows the FIFO queue
- Stack-follows a last-in-first-out stack
- Generic collection
- Using generic collections requires the introduction of namespaces System.Collections.Generic
- list<t>-arrays that can be dynamically added as needed
- Dictionart<tkey, tvalue>-hash table used to store key-value pairs
- Queue<t>-follows the FIFO queue
- Stack<t>-follows a last-in-first-out stack
- ArrayList
- ArrayList is a special array.
- You can dynamically change the length of an array by adding or removing elements
- Flexibility to insert, delete, and access elements
- is not a strong type, slower than a normal array
-
1 usingSystem;2 //using a non-generic collection requires the introduction of namespaces3 usingSystem.Collections;4 5 namespaceArraylistdemo6 {7 class Program8 {9 Public Static voidLog (ArrayList arr)Ten { One stringstr ="the current array has"+ arr. Count +"Elements: ("; A for(inti =0; I < arr. Count; i++) - { -str + =Arr[i]; the if(I < arr.) Count-1) - { -str + =","; - } + } -str + =")"; + Console.WriteLine (str); A } at Static voidMain (string[] args) - { - //1. Create an object first -ArrayList arr =NewArrayList (); - Log (arr); - in //add elements using the Add () method, no restrictions on element types -Arr. ADD ( -); toArr. ADD (2.5f); +Arr. ADD ("Li"); - Log (arr); the * //use [subscript] to get the element at the specified position $ //Console.WriteLine (arr[0]);Panax Notoginseng - //gets the number of elements in the current array the intCount =arr. Count; + A //use the Insert () method to insert an element into a subscript position theArr. Insert (1,"Zhao"); + Log (arr); - $ //remove a specified element from an array using the Remove () method $Arr. Remove ("Zhao"); - Log (arr); - //Use the RemoveAt () method to delete the element that specifies the location of the subscript theArr. RemoveAt (0); - Log (arr);Wuyi the //Use the bool Contains () method to determine whether the specified element exists in the current array - BOOLb = arr. Contains ("Li"); Wu - //empty the entire array About arr. Clear (); $ - } - } -}
"Learning Notes" C # ArrayList