The Array Class |
|
Introduction to the Array Class |
Overview |
In the previous lessons, we saw how to create and initialize arrays. To assist with the use and management of arrays, you can combine the array features of the C# language and support from the .NET Framework. To support arrays, the .NET Framework provides a class of the same name. The Array class is defined in the System namespace of the System.dll assembly. |
When you create an array, you are in fact declaring a variable of type Array. Based on this, since an array variable is an object of a class type, you can use the characteristics of the Array class to create an array and/or to manipulate the values stored in the variable. You can create an array using any of the techniques we saw in the previous lessons, or you can use the Array class. To assist you with creating an array, the Array class is equipped with the CreateInstance() method that comes in various versions. To create a one-dimensional array whose members are zero-based, you can use the following version: |
public static Array CreateInstance(Type elementType, int length); The first argument is used to specify the type of array you want to create. Since it is declared as Type, you can use the typeof operator to cast your type. The second argument specifies the number of members of the array. Using the Array class, you can create an array as follows: using System; public class Exercise { static int Main(string[] args) { Array Numbers = Array.CreateInstance(typeof(double), length); return 0; } } You can also use the var keyword to declare the variable: using System; public class Exercise { static int Main(string[] args) { var Numbers = Array.CreateInstance(typeof(double), length); return 0; } }
We saw that if you declare a variable for an array but don't initialize it, you must specify the number of elements of the array. This number is passed inside the second pair of square brackets, as a constant integer. Here is an example: using System; public class Exercise { static int Main(string[] args) { var Numbers = new double[5]; return 0; } } If you use the Array class to create an array, you must pass this constant integer as the second argument of the CreateInstance() method from the the above version. Here is an example: using System; public class Exercise { static int Main(string[] args) { var Numbers = Array.CreateInstance(typeof(double), 5); return 0; } } If the array exists already, that is, if you have already created the array or you are using an array created by someone else, to find out the number of items it contains, you can access its Length property. Therefore, the length of an array is the number of elements it contains. Alternatively, you can call the Array.GetLength() method. Its syntax is: public int GetLength(int dimension); For a one-dimensional array, you must pass the argument as 0. This method returns a 32-bit integer that represents the number of items in the array.
We have seen that the square brackets are used to specify that you are declaring an array. If you are creating a one-dimensional array, we saw that you could type a number in the square bracket. If you are creating a two-dimensional array, you type two numbers separated by a comma in the second pair of square brackets. Each number, whether it is one, two, or more is a placeholder for what is referred to a dimension. In other words, a one dimensional array has a dimension of one. A two-dimensional array has a dimension of 2. To find out the dimension of an array, the Array class provides the Rank property. Therefore, to know the dimension of an existing array, you can access its Rank.
Before using a class, it must have values or members in it. In the previous lesson, we saw that, to initialize an array, you open the curly brackets and list its members separated by commas, or you could access each member and assign it the desired value. To support the ability to add members to an array, the Array is equipped with a method named SetValue() that comes in different versions. To add a new item to a the type of array we have used so far, you can call the following version of the Array.SetValue() method: public void SetValue(object value, int index); The first argument is the value to add to the list. The second argument is the index of the member to be added. The first item has index 1; the second item has index 2, and so on. Here is an example: using System; public class Exercise { static int Main(string[] args) { var Numbers = Array.CreateInstance(typeof(double), 5); Numbers.SetValue(7628.937, 0); Numbers.SetValue(6.48, 1); Numbers.SetValue(574.9, 2); Numbers.SetValue(293749.064, 3); Numbers.SetValue(0.70257, 4); return 0; } } We indicated that whenever you create an array, you are in fact declaring an instance of the Array class. Therefore, even if you create an array using the square bracket formula we used in the previous lesson, you can call the SetValue() method to specify any member of the array. Here is an example: using System; public class Exercise { static int Main(string[] args) { var Numbers = new double[5]; Numbers.SetValue(7628.937, 0); Numbers.SetValue(6.48, 1); Numbers.SetValue(574.9, 2); Numbers.SetValue(293749.064, 3); Numbers.SetValue(0.70257, 4); return 0; } } The Array class provides a SetValue() version for each corresponding CreateInstance() method we reviewed earlier.
Once the array is initialized, you can access its members and do what you want with their values. To support the ability to retrieve the value of a member of an array, the Array class is equipped with a method named GetValue that is overloaded with a version corresponding to each version of the CreateInstance() and the SetValue() methods. For example, to access the values stored in a one-dimensional array, you can call call this version: public object GetValue(int index); The index argument is the zero-based index of the member whose value you want to access. Here is an example: using System; public class Exercise { static int Main(string[] args) { var Numbers = new double[5]; Numbers.SetValue(7628.937, 0); Numbers.SetValue(6.48, 1); Numbers.SetValue(574.9, 2); Numbers.SetValue(293749.064, 3); Numbers.SetValue(0.70257, 4); Console.WriteLine("Number: {0}", Numbers.GetValue(0)); return 0; } } When calling the Array.GetValue() method, if you pass an invalid value, the compiler would throw an IndexOutOfRangeException exception. Just as you can access one member of the array, you can access any member using its index. Here is an example that uses a for loop and the Length property to know the number of members of an array: using System; public class Exercise { static int Main(string[] args) { //var Numbers = Array.CreateInstance(typeof(double), 5); var Numbers = new double[5]; Numbers.SetValue(7628.937, 0); Numbers.SetValue(6.48, 1); Numbers.SetValue(574.9, 2); Numbers.SetValue(293749.064, 3); Numbers.SetValue(0.70257, 4); for(int i = 0; i < Numbers.Length; i++) Console.WriteLine("Number: {0}", Numbers.GetValue(i)); return 0; } } If using the foreach operator, you don't need the GetValue() method. Here is an example: using System; public class Exercise { static int Main(string[] args) { var Numbers = new double[5]; Numbers.SetValue(7628.937, 0); Numbers.SetValue(6.48, 1); Numbers.SetValue(574.9, 2); Numbers.SetValue(293749.064, 3); Numbers.SetValue(0.70257, 4); foreach(var Number in Numbers) Console.WriteLine("Number: {0}", Number); return 0; } } |
The Array class supports the creation of any of the types of arrays we saw in the previous lessons. In the previous lesson, we saw that a two-dimensional array was an array made of two lists: using System; public static class Exercise { public static int Main(string[] args) { var Members = new string[List1Length, List2Length]; return 0; } } To create such an array using the Array class, you can use the following version of the Array.CreateInstance() method: public static Array CreateInstance(Type elementType, int length1, int length2) The first argument is the type of array you want to create. The second argument is the length of the first list. The third argument is the length of the second list. Here is an example of using it: using System; public static class Exercise { public static int Main(string[] args) { var Members = Array.CreateInstance(typeof(string), 2, 4); return 0; } } To specify the values of a two-dimensional array, you can use the following version of the Array.SetValue() method: public void SetValue(object value, int index1, int index2) The first argument is the value you want to add. The second argument is the index of the list. The second argument is the index of the element that is being added. Here is an example: using System; public static class Exercise { public static int Main(string[] args) { var Members = Array.CreateInstance(typeof(string), 2, 4); Members.SetValue("Celeste", 0, 0); // 1st List - 1st Element Members.SetValue("Mathurin", 0, 1); // 1st List - 2nd Element Members.SetValue("Alex", 0, 2); // 1st List - 3rd Element Members.SetValue("Germain",0, 3); // 1st List - 4th Element Members.SetValue("Jeremy", 1, 0); // 2nd List - 1st Element Members.SetValue("Mathew", 1, 1); // 1st List - 2nd Element Members.SetValue("Anselme", 1, 2); // 1st List - 3rd Element Members.SetValue("Frederique", 1, 3);// 1st List - 4th Element return 0; } } Just as mentioned for the one-dimensional array, you can use the square brackets to create the array but call the SetValue() method to specify the value of each element. To access a member of a two-dimensional array created with the Array.SetValue() method, you use the following version of the Array.GetValue() method: public Object GetValue(int index1, int index2) This method takes two arguments. The first argument is the index of the list where the desired member resides. The second argument is the index of the element itself. Here is an example: using System; public static class Exercise { public static int Main(string[] args) { var Members = Array.CreateInstance(typeof(string), 2, 4); Members.SetValue("Celeste", 0, 0); // 1st List - 1st Element Members.SetValue("Mathurin", 0, 1); // 1st List - 2nd Element Members.SetValue("Alex", 0, 2); // 1st List - 3rd Element Members.SetValue("Germain",0, 3); // 1st List - 4th Element Members.SetValue("Jeremy", 1, 0); // 2nd List - 1st Element Members.SetValue("Mathew", 1, 1); // 1st List - 2nd Element Members.SetValue("Anselme", 1, 2); // 1st List - 3rd Element Members.SetValue("Frederique", 1, 3);// 1st List - 4th Element Console.WriteLine("Member: {0}", Members.GetValue(0, 2)); return 0; } } To access each member of the list, you can use two for loops. Use the first loop to access each list. Nest a second loop to it to access each member. To get the dimension of the main list, you can call the Array.GetLength() method and specify its argument as 0. For the internal loop, pass 1 as the argument to the Array.GetLength() method. Here is an example: using System; public static class Exercise { public static int Main(string[] args) { var Members = Array.CreateInstance(typeof(string), 2, 4); Members.SetValue("Celeste", 0, 0); // 1st List - 1st Element Members.SetValue("Mathurin", 0, 1); // 1st List - 2nd Element Members.SetValue("Alex", 0, 2); // 1st List - 3rd Element Members.SetValue("Germain",0, 3); // 1st List - 4th Element Members.SetValue("Jeremy", 1, 0); // 2nd List - 1st Element Members.SetValue("Mathew", 1, 1); // 1st List - 2nd Element Members.SetValue("Anselme", 1, 2); // 1st List - 3rd Element Members.SetValue("Frederique", 1, 3);// 1st List - 4th Element for(int List = 0; List < Members.GetLength(0); List++) for(int Element = 0; Element < Members.GetLength(1); Element++) Console.WriteLine("Member: {0}", Members.GetValue(List , Element )); return 0; } } You can also use a foreach operator to access each member of the array. When using it, there is no need for a counter. Here is an example: using System; public static class Exercise { public static int Main(string[] args) { var Members = Array.CreateInstance(typeof(string), 2, 4); Members.SetValue("Celeste", 0, 0); // 1st List - 1st Element Members.SetValue("Mathurin", 0, 1); // 1st List - 2nd Element Members.SetValue("Alex", 0, 2); // 1st List - 3rd Element Members.SetValue("Germain",0, 3); // 1st List - 4th Element Members.SetValue("Jeremy", 1, 0); // 2nd List - 1st Element Members.SetValue("Mathew", 1, 1); // 1st List - 2nd Element Members.SetValue("Anselme", 1, 2); // 1st List - 3rd Element Members.SetValue("Frederique", 1, 3);// 1st List - 4th Element foreach (string Member in Members) Console.WriteLine("Member: {0}", Member); return 0; } }
Instead of two dimensions, you may want to create a three-dimensional arrays. A 3-D array is an array that, if created with the square brackets, would use two commas. Here is an example: using System; public static class Exercise { static int Main(string[] args) { double[,,] Number; return 0; } } To create such an array using the Array class, you can use the following version of its CreateInstance() method: public static Array CreateInstance(Type elementType, int length1, int length2, int length3) Here is an example: using System; public static class Exercise { static int Main(string[] args) { var Number = Array.CreateInstance(typeof(double), 2, 3, 5); return 0; } } To specify the value of each member of the three-dimensional array, you can call the following version of the Array.SetValue() method: public void SetValue ( Object value, int index1, int index2, int index3 ) Here is an example: using System; public static class Exercise { static int Main(string[] args) { var Number = Array.CreateInstance(typeof(double), 2, 3, 5); Number.SetValue( 12.44, 0, 0, 0); Number.SetValue( 525.38, 0, 0, 1); Number.SetValue( -6.28, 0, 0, 2); Number.SetValue(2448.32, 0, 0, 3); Number.SetValue( 632.04, 0, 0, 4); Number.SetValue(-378.05, 0, 1, 0); Number.SetValue( 48.14, 0, 1, 1); Number.SetValue( 634.18, 0, 1, 2); Number.SetValue( 762.48, 0, 1, 3); Number.SetValue( 83.02, 0, 1, 4); Number.SetValue( 64.92, 0, 2, 0); Number.SetValue( -7.44, 0, 2, 1); Number.SetValue( 86.74, 0, 2, 2); Number.SetValue(-534.60, 0, 2, 3); Number.SetValue( 386.73, 0, 2, 4); Number.SetValue( 48.02, 1, 0, 0); Number.SetValue( 120.44, 1, 0, 1); Number.SetValue( 38.62, 1, 0, 2); Number.SetValue( 526.82, 1, 0, 3); Number.SetValue(1704.62, 1, 0, 4); Number.SetValue( 56.85, 1, 1, 0); Number.SetValue(105.48, 1, 1, 1); Number.SetValue( 363.31, 1, 1, 2); Number.SetValue( 172.62, 1, 1, 3); Number.SetValue( 128.48, 1, 1, 4); Number.SetValue( 906.68, 1, 2, 0); Number.SetValue( 47.12, 1, 2, 1); Number.SetValue(-166.07, 1, 2, 2); Number.SetValue(4444.26, 1, 2, 3); Number.SetValue( 408.62, 1, 2, 4); return 0; } } To get the value of each member of the three-dimensional array, you can call the following version of the Array.GetValue() method: public Object GetValue ( int index1, int index2, int index3 ) Here is an example: using System; public static class Exercise { static int Main(string[] args) { var Number = Array.CreateInstance(typeof(double), 2, 3, 5); Number.SetValue( 12.44, 0, 0, 0); Number.SetValue( 525.38, 0, 0, 1); Number.SetValue( -6.28, 0, 0, 2); Number.SetValue(2448.32, 0, 0, 3); Number.SetValue( 632.04, 0, 0, 4); Number.SetValue(-378.05, 0, 1, 0); Number.SetValue( 48.14, 0, 1, 1); Number.SetValue( 634.18, 0, 1, 2); Number.SetValue( 762.48, 0, 1, 3); Number.SetValue( 83.02, 0, 1, 4); Number.SetValue( 64.92, 0, 2, 0); Number.SetValue( -7.44, 0, 2, 1); Number.SetValue( 86.74, 0, 2, 2); Number.SetValue(-534.60, 0, 2, 3); Number.SetValue( 386.73, 0, 2, 4); Number.SetValue( 48.02, 1, 0, 0); Number.SetValue( 120.44, 1, 0, 1); Number.SetValue( 38.62, 1, 0, 2); Number.SetValue( 526.82, 1, 0, 3); Number.SetValue(1704.62, 1, 0, 4); Number.SetValue( 56.85, 1, 1, 0); Number.SetValue(105.48, 1, 1, 1); Number.SetValue( 363.31, 1, 1, 2); Number.SetValue( 172.62, 1, 1, 3); Number.SetValue( 128.48, 1, 1, 4); Number.SetValue( 906.68, 1, 2, 0); Number.SetValue( 47.12, 1, 2, 1); Number.SetValue(-166.07, 1, 2, 2); Number.SetValue(4444.26, 1, 2, 3); Number.SetValue( 408.62, 1, 2, 4); Console.WriteLine("Number: {0}\n", Number.GetValue(0, 2, 4)); return 0; } } This would produce: Number: 386.73 Press any key to continue . . . To access each member of the array, you can use three for loops. Here is an example: using System; public static class Exercise { static int Main(string[] args) { var Number = Array.CreateInstance(typeof(double), 2, 3, 5); Number.SetValue( 12.44, 0, 0, 0); Number.SetValue( 525.38, 0, 0, 1); Number.SetValue( -6.28, 0, 0, 2); Number.SetValue(2448.32, 0, 0, 3); Number.SetValue( 632.04, 0, 0, 4); Number.SetValue(-378.05, 0, 1, 0); Number.SetValue( 48.14, 0, 1, 1); Number.SetValue( 634.18, 0, 1, 2); Number.SetValue( 762.48, 0, 1, 3); Number.SetValue( 83.02, 0, 1, 4); Number.SetValue( 64.92, 0, 2, 0); Number.SetValue( -7.44, 0, 2, 1); Number.SetValue( 86.74, 0, 2, 2); Number.SetValue(-534.60, 0, 2, 3); Number.SetValue( 386.73, 0, 2, 4); Number.SetValue( 48.02, 1, 0, 0); Number.SetValue( 120.44, 1, 0, 1); Number.SetValue( 38.62, 1, 0, 2); Number.SetValue( 526.82, 1, 0, 3); Number.SetValue(1704.62, 1, 0, 4); Number.SetValue( 56.85, 1, 1, 0); Number.SetValue(105.48, 1, 1, 1); Number.SetValue( 363.31, 1, 1, 2); Number.SetValue( 172.62, 1, 1, 3); Number.SetValue( 128.48, 1, 1, 4); Number.SetValue( 906.68, 1, 2, 0); Number.SetValue( 47.12, 1, 2, 1); Number.SetValue(-166.07, 1, 2, 2); Number.SetValue(4444.26, 1, 2, 3); Number.SetValue( 408.62, 1, 2, 4); for(int External = 0; External < Number.GetLength(0); External++) for(int Internal = 0; Internal < Number.GetLength(1); Internal++) for(int Element = 0; Element < Number.GetLength(2); Element++) Console.WriteLine("Number: {0}", Number.GetValue(External, Internal, Element)); return 0; } } This would produce: Number: 12.44 Number: 525.38 Number: -6.28 Number: 2448.32 Number: 632.04 Number: -378.05 Number: 48.14 Number: 634.18 Number: 762.48 Number: 83.02 Number: 64.92 Number: -7.44 Number: 86.74 Number: -534.6 Number: 386.73 Number: 48.02 Number: 120.44 Number: 38.62 Number: 526.82 Number: 1704.62 Number: 56.85 Number: 105.48 Number: 363.31 Number: 172.62 Number: 128.48 Number: 906.68 Number: 47.12 Number: -166.07 Number: 4444.26 Number: 408.62 Press any key to continue . . . You can also use a foreach loop to access each member of the array. Here is an example: using System; public static class Exercise { static int Main(string[] args) { var Number = Array.CreateInstance(typeof(double), 2, 3, 5); Number.SetValue( 12.44, 0, 0, 0); Number.SetValue( 525.38, 0, 0, 1); Number.SetValue( -6.28, 0, 0, 2); Number.SetValue(2448.32, 0, 0, 3); Number.SetValue( 632.04, 0, 0, 4); Number.SetValue(-378.05, 0, 1, 0); Number.SetValue( 48.14, 0, 1, 1); Number.SetValue( 634.18, 0, 1, 2); Number.SetValue( 762.48, 0, 1, 3); Number.SetValue( 83.02, 0, 1, 4); Number.SetValue( 64.92, 0, 2, 0); Number.SetValue( -7.44, 0, 2, 1); Number.SetValue( 86.74, 0, 2, 2); Number.SetValue(-534.60, 0, 2, 3); Number.SetValue( 386.73, 0, 2, 4); Number.SetValue( 48.02, 1, 0, 0); Number.SetValue( 120.44, 1, 0, 1); Number.SetValue( 38.62, 1, 0, 2); Number.SetValue( 526.82, 1, 0, 3); Number.SetValue(1704.62, 1, 0, 4); Number.SetValue( 56.85, 1, 1, 0); Number.SetValue(105.48, 1, 1, 1); Number.SetValue( 363.31, 1, 1, 2); Number.SetValue( 172.62, 1, 1, 3); Number.SetValue( 128.48, 1, 1, 4); Number.SetValue( 906.68, 1, 2, 0); Number.SetValue( 47.12, 1, 2, 1); Number.SetValue(-166.07, 1, 2, 2); Number.SetValue(4444.26, 1, 2, 3); Number.SetValue( 408.62, 1, 2, 4); foreach(double n in Number) Console.WriteLine("Number: {0}", n); return 0; } }
The Array class supports all dimensions of arrays beyond three. To create a multidimensional array, the class is equipped with the following version of its CreateInstance() method: public static Array CreateInstance(Type elementType, params int[] lengths) To add elements to the list, you can use the following equivalent version of the SetValue() method: public void SetValue(object value, params int[] indices) To get the value of an element, you would call the following version of the GetValue() method: public Object GetValue(params int[] indices)
When you initialize an array, you add the members in an order of your choice. At one point, when accessing the members of an array, you may want them to be arranged in alphabetical, in numerical, or in chronological order. To assist you with re-arranging the elements in an array, the Array class is equipped with a method named Sort that is overloaded with as many versions as you can possibly need. To arrange an array of the type we have used so far, you can call the following version of the Array.Sort() method: public static void Sort(Array array); This is a static method that takes as argument the name of the array you want to re-arrange. Here is an example: using System; public class Exercise { static int Main(string[] args) { var Numbers = new double[] { 7628.937, 6.48, 574.9, 293749.064, 0.70257, 314.905, 80458.01 }; Console.WriteLine("List of Numbers"); foreach(var Number in Numbers) Console.WriteLine("Number: {0}", Number); Array.Sort(Numbers); Console.WriteLine("\nList of Numbers"); foreach (var Number in Numbers) Console.WriteLine("Number: {0}", Number); return 0; } } This would produce: List of Numbers Number: 7628.937 Number: 6.48 Number: 574.9 Number: 293749.064 Number: 0.70257 Number: 314.905 Number: 80458.01 List of Numbers Number: 0.70257 Number: 6.48 Number: 314.905 Number: 574.9 Number: 7628.937 Number: 80458.01 Number: 293749.064 Press any key to continue . . . Notice that the numbers are arranged in in ascending order. In the same way, if the array is made of strings, you can call the Array.Sort() method to arrange it in alphabetical order. If the array is made of dates, you can arrange them in chronological order.
To arrange the members in reverse order, you can call the Array.Reverse() method. Its syntax is: public static void Reverse(Array array);
One of the most routine operations you can perform on an array is to find out whether it contains this or that value. For example, if the array contains a certain member, you may want to retrieve the index of that member. To assist you with this, the Array class is equipped with a method named IndexOf() method that comes in various versions. To apply it on the type of array we have used so far, you can use the following syntax: public static int IndexOf(Array array, object value); This method visits each member of the array, looking for the value. Once it finds value in the array, it stops and returns the index where the first occurrence of value was found. Here is an example of calling it: using System; public class Exercise { static int Main(string[] args) { var Numbers = new double[] { 7628.937, 6.48, 574.9, 293749.064, 0.70257, 314.905, 80458.01 }; Console.WriteLine("List of Numbers"); foreach(var Number in Numbers) Console.WriteLine("Number: {0}", Number); Console.WriteLine(); int Index = Array.IndexOf(Numbers, 314.905); Console.WriteLine("The index of 314.905 is {0}", Index); return 0; } } If the Array.IndexOf() method finds the value in the array, it returns its position. The above program produces: List of Numbers Number: 7628.937 Number: 6.48 Number: 574.9 Number: 293749.064 Number: 0.70257 Number: 314.905 Number: 80458.01 The index of 314.905 is 5 Press any key to continue . . . If the array is not found, the method may return -1. The IndexOf() method actually looks for the first occurrence of an item in an array. If you prefer to get the last occurrence of that item in the array, you can call the Array.LastIndexOf() method. It also is overloaded in three versions.
To better manage an array, the compiler must always be able to locate its highest and its lowest members. This is particularly important because an array must have a size. The lowest member of an array can be located using the Array.GetLowerBound() method. Its syntax is: public int GetLowerBound(int dimension); The highest member of an array can be located using the Array.GetUpperBound() method. Its syntax is: public int GetUpperBound(int dimension); In both cases, the dimension argument is the rank of the array. For a single-dimensional array, as those we have always used so far, this parameter must have the value of 0.
|
|
||
Previous | Copyright © 2008-2016, FunctionX, Inc. | Next |
|