Topic description
Add, delete, traverse and find elements of the ArrayList class. (console application)
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections;//Add namespace Addition of namespace ArrayList class elements { class Program { static void Main(string[] args) { ArrayList myArrayList=new ArrayList(5); //The advantage of ArrayList is that the length is not fixed and the type is arbitrary //The length of the array is fixed and cannot be changed. The type is single and can only be one of them Console.WriteLine("myArrayList has {0} elements after initialization", myArrayList.Count); //Add method is used to add a single element to the ArrayList, only one can be added at a time myArrayList.Add(123); myArrayList.Add('a'); myArrayList.Add("myArrayList"); myArrayList.Add(25.6); myArrayList.Add(10L); Console.WriteLine("After adding 5 personal elements using the Add method, there are {0} elements", myArrayList.Count); //AddRange(myArrayList) method is used to add multiple elements to ArrayList at one time, which can be an array string[] mystringArray = { "Zhang San", "Li Si", "Wang Wu" , "Zhao Liu"}; myArrayList.AddRange(mystringArray); Console.WriteLine("After using the AddRange method, there are {0} elements",myArrayList.Count); // loop through the collection elements //The reference type string\object class is the base class of all types foreach(object outelement in myArrayList) Console.Write(outelement+"\t"); Console.WriteLine(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections;//Add namespace Deletion of namespace ArrayList class elements { class Program { static void Main(string[] args) { //There are four ways to delete elements ArrayList myarrayList = new ArrayList(3); myarrayList.Add(1); myarrayList.Add("成功"); myarrayList.Add(25.6); string [] mystringArray={"Zhang San", "Li Si", "Wang Wu", "Zhao Liu"}; myarrayList.AddRange(mystringArray); Console.WriteLine("Delete previous content: "); foreach(object outelement in myarrayList) Console.Write(outelement+"\t"); Console.WriteLine(); //Remove(value) Console.WriteLine("Delete the content"); myarrayList.Remove("张三"); foreach (object outelement in myarrayList) Console.Write(outelement + "\t"); Console.WriteLine(); //RemoveAt(index value) Console.WriteLine("Delete the value after Li Si"); myarrayList.RemoveAt(3); foreach (object outelement in myarrayList) Console.Write(outelement + "\t"); Console.WriteLine(); //RemoveRange (starting index, number to delete) Console.WriteLine("Delete the value after Wang Wu and Zhao Liu: "); myarrayList.RemoveRange(3, 2); foreach (object outelement in myarrayList) Console.Write(outelement + "\t"); Console.WriteLine(); //Clear() clears all elements Console.WriteLine("Clear all elements: "); myarrayList.Clear(); foreach (object outelement in myarrayList) Console.Write(outelement + "\t"); Console.WriteLine(); } } }
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Collections;//Add namespace namespace ArrayList class element traversal and search { class Program { static void Main(string[] args) { ArrayList myArrayList = new ArrayList(2); myArrayList.Add("数字:"); int[] myintArray = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; myArrayList.AddRange(myintArray); myArrayList.Add("String:"); string[] mystringArray = { "Zhang San", "Li Si", "Wang Wu", "Zhao Liu" }; myArrayList.AddRange(mystringArray); foreach (object outelement in myArrayList) Console.Write(outelement + "\t"); // find element //There are three methods //IndexOf (the element to find), returns an index value of the first occurrence, or -1 if it does not exist Console.WriteLine(myArrayList.IndexOf("张三")); //LastIndexOf (the element to find), returns an index value of the last occurrence, or -1 if it does not exist Console.WriteLine(myArrayList.LastIndexOf(20)); //BinarySearch(), the type is inconsistent, sometimes an error is reported Console.WriteLine(myArrayList.BinarySearch(2)); } } }