Easily learn the ArrayList class of C

  • 2021-08-21 21:13:27
  • OfStack

The dynamic array ArrayList class is under the namespace System. Collecions, so it is used with the namespace System. Collecions, and ArrayList provides methods to add, insert, or remove a range of 1 elements. In ArrayList, users can only get or set the value of one element at a time.
1. Adding ArrayList Elements
ArrayList provides two methods for adding elements to ArrayList, Add and AddRange.
(1) The Add method adds a single element to the end of the list in the format: ArrayList Object. Add (the value to add)
(2) The AddRange method takes an instance of the collection that implements the ICollection interface and sequentially adds it to the end of the list in the format: ArrayList Object. AddRange (the array to be added)
Example 1. Adding elements and arrays to arrays by the above methods


<span style="font-size:18px;">using System; 
using System.Collections;// Namespace to be added  
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace  Use of dynamic arrays  
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
  ArrayList al = new ArrayList(3);// Defined 1 Dynamic arrays and the initial array element number is 3 A  
  Console.WriteLine(" Before adding al The number of elements of is: "+al.Count); 
  al.Add("abc"); 
  al.Add("xyz"); 
  al.Add("opq"); 
  Console.WriteLine(" Call Add After method al The number of elements of is: "+al.Count); 
  string[] last = { "def", "ghj" }; 
  al.AddRange(last); 
  Console.WriteLine(" Call AddRange After method al The number of elements of is: "+al.Count); 
  foreach (string item in al) 
  { 
  Console.WriteLine(item); 
  } 
  Console.ReadLine(); 
 } 
 } 
}</span> 

The output result is: The number of elements without adding al is: 0
The number of al elements after calling the Add method is: 3
The number of elements of al after calling AddRange method is: 5
abc xyz opq def ghj (1 per line)
2. ArrayList Element Deletion
ArrayList provides four methods for deleting elements from ArrayList. These four methods are Remove, RemoveAt, RemoveRange and Clear.
The Remove method accepts a parameter of type object to remove the first matching collection element of the specified element value. The format is: ArrayList Object. Remove (Value)
The RemoveAt method accepts a parameter of type int to delete the collection element of the specified index. The format is: ArrayList Object. RemoveAt (Index)
The RemoveRange method removes 1 ranged element from the collection. The format is: ArrayList Object. RemoveRange (Start Index, Number to Delete)
The Clear method clears all elements.
Example 2. Deleting elements by the above method


<span style="font-size:18px;">using System; 
using System.Collections;// Namespace to be added  
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace  Use of dynamic arrays  
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
  ArrayList al = new ArrayList(3);// Defined 1 Dynamic arrays and the initial array element number is 3 A  
  al.Add("abc"); 
  al.Add(50); 
  al.Add(10); 
  string[] last = { "def", "ghj" }; 
  al.AddRange(last); 
  Console.WriteLine(" Before deleting al The number of elements of is: " + al.Count); 
  al.RemoveAt(2);// Drop the index as 2 Element after  
  Console.WriteLine(" Drop the index as 2 The number of elements after is: "+al.Count); 
  al.Remove("abc");// Delete the 1 The value is abc Items of  
  Console.WriteLine(" The delete value is abc The number of elements after is: "+al.Count); 
  al.RemoveRange(1,2);// Delete self-indexed 1 Two elements of  
  Console.WriteLine(" Delete self-indexed 1 Number of elements after two elements of: "+al.Count); 
  foreach (string item in al)// Because the element type in this object does not 1 To cause it to be object Type  
  { 
  Console.WriteLine(item); 
  } 
  Console.ReadLine(); 
 } 
 } 
}</span> 

The output result is: The number of elements of al before deletion is: 5
The number of elements after deleting index 2 is: 4
The number of elements after deleting abc is: 3
Number of elements after deleting two elements with index 1: 1
xyz
3. Lookup of ArrayList Elements
ArrayList element search provides three methods to find elements in ArrayList, which are IndexOf method, LastindexOf method and BinarySearch method.
(1) The IndexOf method searches the specified string from before and after. If it is found, it returns the index starting from 0 of the matching item 1, otherwise it returns-1. The format is: ArrayList Object. IndexOf (String to Index)
(2) The LastIndexOf method searches the specified string from back to front. If it is found, it returns the index of the last matching item starting from 0, otherwise it returns-1. Its format is: ArrayList Object. LastIndexOf (the string to be indexed)
Both methods have three overloaded versions, which indicate that the search starts at the specified index or searches for a string of the specified length at the specified index.
(3) The BinarySearch method uses a binary algorithm to specify a value from the collection and returns the index found starting at 0, otherwise returns-1 in the format: ArrayList Object. BinarySearch (string to index)
Example 3. Finding the specified element using the above method


<span style="font-size:18px;">using System; 
using System.Collections;// Namespace to be added  
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 
 
namespace  Use of dynamic arrays  
{ 
 class Program 
 { 
 static void Main(string[] args) 
 { 
  string[] str = { "a", "b", "c", "d", "d", "e", "f" }; 
  ArrayList al = new ArrayList(str); 
  int i = al.IndexOf("c");// Find the first 1 Characters c Position in the array  
  Console.WriteLine(" Element c The position in the collection is: "+i); 
  i = al.LastIndexOf("d");// Find the last 1 Characters d Position in the array  
  Console.WriteLine(" Element d The position in the collection is: " + i); 
  int j = al.BinarySearch("f");// Find Elements f Position in the array  
  if (j>0) 
  { 
  Console.WriteLine(" Element f The position in the array is: "+j); 
  } 
  else 
  { 
  Console.WriteLine(" Not found a"); 
  } 
  Console.ReadLine(); 
 } 
 } 
}</span> 

The output is that the position of the element c in the collection is: 2
The position of the element d in the collection is: 3
The position of element f in the array is: 5
4. Traversal of ArrayList Elements
In the process of executing the above program, the foreach statement has been used to traverse the ArrayList element, so no example will be given here.

The above is about C # ArrayList class related introduction, I hope to help you learn.


Related articles: