. NET Framework provides specialized classes for data storage and retrieval. These classes are collectively referred to as collections. These classes provide support for stacks, queues, lists, and hash tables. Most collection classes implement system interfaces. The following describes the ArrayList.
ArrayList is a part of the namespace Systrm. Collections. It uses an array that can be dynamically increased by size as needed to implement the IList interface.
The ArrayList capacity is the number of elements that can be saved in ArrayList. The default initial capacity of ArrayList is 0. As the element is added to ArrayList, the capacity will automatically increase as needed through reallocation. You can use the certificate index to access the elements in this set. The index in this set starts from scratch.
Here is an example:
Using System; using System. collections. generic; using System. linq; using System. text; using System. threading. tasks; using System. collections; namespace collection {// Animal class public abstract class Animal {protected string name; // Animal Name public string name {get {return name ;}set {name = value ;}} public Animal () {name = "The animal with no name";} public Animal (string newName) {name = newName;} public void Feed () // breeding descendant {Console. writeLine ("{0} has been fed. ", name) ;}// Cow class Cow: Animal {public Cow (string newName): base (newName) {} public void Milk () // milk production {Console. writeLine ("{0} has been milked. ", name) ;}// hen class public class Chicken: Animal {public Chicken (string newName): base (newName) {} public void LayEgg () // egg {Console. writeLine ("{0} has laid an egg. ", name) ;}} class Program {static void Main (string [] args) {// use the array method to implement the Console. writeLine ("Create an Array type collection of Animal object and use it:"); Animal [] animalArray = new Animal [2]; Cow myCow1 = new Cow ("deirsid "); animalArray [0] = myCow1; animalArray [1] = new Chicken ("Ken"); foreach (Animal myAnimal in animalArray) {Console. writeLine ("New {0} object added to Array collection, Name = {1}", myAnimal. toString (), myAnimal. name);} Console. writeLine ("Array collection contains {0} objects. ", animalArray. length); animalArray [0]. feed (); (Chicken) animalArray [1]). layEgg (); Console. writeLine (); // use the set method to implement the Console. writeLine ("Create an ArrayList type collection of Animal objects and use it;"); ArrayList animalArrayList = new ArrayList (); Cow myCow2 = new Cow ("HayLey"); animalArrayList. add (myCow2); animalArrayList. add (new Chicken ("Roy"); foreach (Animal myAnimal in animalArrayList) {Console. writeLine ("New {0} object added to ArrayList collection, Name = {1}", myAnimal. toString (), myAnimal. name);} Console. writeLine ("ArrayList collection contains {0} objects. ", animalArrayList. count); (Animal) animalArrayList [0]). feed (); (Chicken) animalArrayList [1]). layEgg (); Console. writeLine ();}}}
Set Creation <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> Vc3ryb25np1_vcd4kpha + forward + CjxzdHJvbmc + ttTT2rzytaW1xMr91 + forward/K/forward = "brush: java;"> Animal [] animalArray = new Aimal [2];
The ArrayList set does not need to initialize its size.
ArrayList animalArrayList = new ArrayList();
This class also has two constructors. The first constructor uses the existing set as a parameter to copy the content of the existing set to the new instance. The other constructor uses one parameter to set the capacity of the set ). This capacity is specified with an int value to set the number of initial items that can be included in the set. But this is not the actual capacity, because if the number of items in the Set exceeds this value, the capacity will automatically double.
For the ArrayList set, there is no ready-made item or null referenced item. We use the Add () method of the ArrayList object to Add a new item:
Cow myCow2 = new Cow("Hayley");anmialArrayList.Add(myCow2);anmialArrayList.Add(new Chicken("Roy"));
The ArrayList object supports the IEnumerable interface. The unique method of this interface, GetEnuumerator (), can iterate items in the set. So it can be used with foreach
foreach (Animal myAnimal in animalArrayList){Console.WriteLine("New {0} object added to ArrayList collection,Name = {1}",myAnimal.Tostring(),myAnimal.Name);}
The ArrayList set is a collection of System. Object objects (assigned to Animal objects through polymorphism). Therefore, all items must be used for data type conversion:
((Animal)animalArrayList[0]).Feed();((Chichen)animalArrayList[1]).LayEgg();
You can use the Remove () and RemoveAt () Methods to delete items. These two methods are part of the IList interface implemented in the ArrayList class. They delete items from the set based on the item reference or index:
animalArrayList.RemoveAt(0);animalArrayList.Remove(myCow2);
You can use the AddRange () method to add several items to the ArrayList set at a time. This method accepts any object with the ICollection interface, including Arrays:
animalArrayList.AddRange(animalArray);
The AddRange () method is not part of any interface provided by ArrayList. This method is dedicated to the ArrayList class,