1. What is an array?
A: arrays are a group.Same data type andSet of data that can be indexed.
2. What is the difference between array and arraylist?
A: The array isFixed sizeWhile the arraylist isVariable Size.
1. Use of arrays (applicable to fixed sizes)
Namespace arraydemo1 {class program {static void main (string [] ARGs) {// 1. The array declaration and initialization come in two ways. // Method 1: conventional method // string [] names = new string [5]; // specify the number of array elements // Names [0] = ""; // Names [1] = "B"; // Names [2] = "C"; // Names [3] = "D "; // Names [4] = "e"; // Method 2: object initialization list string [] names = new string [] {"A", "B ", "C", "D", "E"}; // method first-class price // 2, array element setting and access, there are two methods // Method 1: direct access method names [2] = "cc"; // set string myname = Names [2]; // access // call setvalue () and getvalue () of the array class () method // names. setvalue ("cc", 2); // set // string myname1 = (string) names. getvalue (2); // access, note that the type returned by getvalue is object // 3, and the array traversal foreach (string name in names) // you can also use for loop traversal {console. write (name + "");} console. readkey (); // program output result:/* a B CC D E */}}}
2. Use of arraylist (applicable to dynamically increasing arrays)
Namespace arraylistdemo1 {class program {static void main (string [] ARGs) {// 1, create an arraylist instance (object) arraylist grades = new arraylist (); // you do not need to specify the data item size. // 2. There are two ways to add items. // Method 1: Add () method grades. add (100); // you can add only one item at a time and add it at the end of grades. insert (); // Add to the specified position // Method 2: The addrange () method grades. addrange (New int [] {90, 80, 70}); // note that this method parameter is an icollection object, that is, the icollection interface is implemented. // 3. Remove items, there are two methods // Method 1: Remove () method if (grades. contains (90 ))// Check whether the object exists {grades. Remove (90); console. writeline ("90 be removed! ");} Else {console. writeline (" object not in arraylist! ");} // Method 2: removeat () method, applicable to int Pos = 0 when the element index is known; Pos = grades. indexof (80); // use the indexof () method to determine the position (INDEX) of the element to be removed grades. removeat (POS); console. writeline ("80 be removed! "); // 4, traverse the console. writeline ("The list of grades:"); foreach (Object grade in grades) {console. write (grade + "");} console. readkey (); // program output result:/* 100 95 70 */}}}