Some methods of array
Int[] Nums = {5, 4, 6, 3, 14, 9, 8, 17, 1, 24,-1, 0};
Console.Write ("Order before Ordering:");
foreach (int i in nums)
Console.Write (i + ""); Console.WriteLine ();
int before = Array.indexof (Nums, 14);
Console.WriteLine ("Before sort index is" + before);
Ordinal number in the array before sorting
Array.Sort (nums);//static methods sort by array
Console.Write ("Order after Ordering:");
foreach (int i in nums) Console.Write (i + "");
Output array int index = Array.BinarySearch (nums, 14);
The ordinal number of the lookup Console.WriteLine ("The index is" + index);
Console.WriteLine (); string[] Strarray = new string[]
{"75.3", "25.999", "105.25"};
Type conversion double[] Doublearray = Array.convertall
<string, Double> (Strarray, convert.todouble);
Conversion of implementation types
Console.Write ("Converted to doubles:");
Array.foreach<double> (Doublearray, delegate (double x)
{Console.Write (x + "");}); /Delegate Events
Screenshot of program results
Second, ArrayList
Array is a static array, and once the size of the arrays is determined in the initialization process, it cannot be modified at the end, and the elements in the arrays cannot be added and deleted. This is not very flexible, for this C # provides ArrayList for handling dynamic arrays.
ArrayList after the creation can be based on the actual needs of the element additions and deletions. This is very useful. Let's look at the next ArrayList constructor.
ArrayList al = new ArrayList ();
Let's look at a simple example:
ArrayList al = new ArrayList ();
Console.WriteLine
("Initial Number of elements:" + al. Count);
Console.WriteLine ("Adding 6 elements");
ADD elements to the array list
Al. ADD (' C ');
Al. ADD (' A ');
Al. ADD (' E ');
Al. ADD (' B ');
Al. ADD (' D ');
Al. ADD (' F ');
Al. Remove (' a ');//Delete an element Console.WriteLine (al. Count); Al. Add (' a ');//Add an element Console.WriteLine (al. Count); Al[0] = ' X ';//modify array element Al[1] = ' Y '; AL[2] = ' Z '; |
After you delete an element, the number becomes 5, and the number of elements added is 6. |
So now let's build a digital ArrayList to learn how to do it;
ArrayList al = new ArrayList ();
ADD elements to the array list
Al. ADD (155); Al. ADD (413);
Al. ADD (-41); Al. ADD (818);
Al. ADD (31); Al. ADD (191);
Console.Write ("Original contents:");
foreach (int i in AL)
Console.Write (i + "");
Console.WriteLine ("\ n");
Sort sorting
Al. Sort ();
Console.Write ("Contents after sorting:");
foreach (int i in AL)
Console.Write (i + "");
Console.WriteLine ("\ n");
Console.WriteLine ("Index of 413 is" +
Al. BinarySearch (413));
The above figure is the result of the output, this time to ArrayList only made a simple introduction, the next to ArrayList provided by the instance method and static method to do a detailed introduction.