Arrays
An array is a group of elements of the same type that have a common name.
An array is a collection of elements that use the same name but are distinguished by an index value.
Arrays contain a sequence of variables.
Different Types of Arrays
There are several different types of arrays:
Fixed size array which remains the same size and a
Dynamic size array whose size can change at run-time.
Rectangular Arrays - These are really 2 dimensional. The number of rows is identical to the number of columns.
Jagged Arrays - These are not really 2 dimensional and represent a 1 dimensional array of 1 dimensional arrays. Each element in the main array can hold an array of varying length.
Zero Based
All arrays are zero based
The following line declares an array containing 10 elements from 0 to 9.
Dim aiArrayIntegers(10) As Integer
Because the first element of every array has a zero index the "To" keyword inside a Dim statement is no longer supported.
The Option Base statement is no longer supported.
Accessing Elements
Another method you may use frequently is the GetValue method.
'Will return the value of the 9th element if there is one.
int myValue = myArray.GetValue(9);
We can access an array element by passing the item index to the array
int myValue = myArray[0];
int myValue = myArray[1];
string[] myArray;
string[] myArray = new string[3];
myArray[0] = 5;
myArray[1] = 10;
myArray.SetValue(15,2);
Passing Arrays as arguments
void Main(string{} args)
Sub Main(ByVal args() As String)
Returning Arrays
Public int[] GetNumber()
{
int[] ArrayInt = new int[3];
ArrayInt[0] = 5;
ArrayInt[1] = 10;
ArrayInt[2] = 15;
return ArrayInt
}
Arrays are collections of objects of the same type.
The size must be decided when the array is created.
Each item in the array is accessed by an index, which is just a number that indicates the position where the object is stored.
Arrays can be used to store both Reference Types and Value Types.
Arrays use zero-based indexes, so that the first element in the array is element 0.
Single-Dimensional Arrays
An array is an indexed collection of objects.
A single-dimensional array of objects is declared like this:
type[] arrayName;
int[] array = new int[5];
int[] array1 = new int[] { 1, 3, 5, 7, 9 };
int[] array2 = {1, 3, 5, 7, 9};
string[] days = {"Sun", "Mon", "Tue", "Wed", "Thr", "Fri", "Sat"};
The default value of numeric array elements is zero
The default value of any reference elements is null
Multidimensional Arrays
You are not limited to just 1-dimensional arrays in .NET
Both C# and VB.NET let you create arrays with multiple dimensions
Arrays with mutiple dimensions are also referred to as jagged arrays.
string[,] asMulti = new string[5,5];
Dim asMulti(5,5) As String
GetLowerBound - Get the upper bound of the specified dimension. Starting at the number 0.
GetUpperBound - Get the lower bound of the specified dimension. Starting at the number 0.
Dim asTableValues(10, 200) As String
Dim asTableValues As String()
asTableValues.GetUpperBound(0) = 10
asTableValues.GetUpperBound(1) = 200
Dim icolumnnumber As Integer
For icolumnnumber = 0 To asArrayName.GetUpperBound(0)
Next icolumnnumber
Dim irowcount As Integer
For irowcount = 0 To asArrayName.GetUpperBound(1)
Next irowcount
Conceptually, a multidimensional array with two dimensions resembles a grid. A multidimensional array with three dimensions resembles a cube.
int[,] array2D = new int[2,3];
int[,] array2D2 = { {1, 2, 3}, {4, 5, 6} };
for (int i=0; i<2; i++)
{
for (int j=0; j<3; j++)
{
array2D[i,j] = (i + 1) * (j + 1);
}
}
for (int i=0; i<2; i++)
{
for (int j=0; j<3; j++)
{
System.Console.Write(array2D[i,j]);
}
System.Console.WriteLine();
}
int[] numbers = { 4, 5, 6, 1, 2, 3, -2, -1, 0 };
foreach (int i in numbers)
{
System.Console.Write("{0} ", i);
}
Jagged Arrays
A variation of the multidimensional array is the jagged array: an array of arrays.
A jagged array is a single-dimensional array, and each element is itself an array.
The element arrays are not required to all be of the same size.
You declare a jagged array like this:
int[][] jaggedArray = new int[3][];
jaggedArray[0] = new int[5];
jaggedArray[1] = new int[4];
jaggedArray[2] = new int[2];
© 2023 Better Solutions Limited. All Rights Reserved. © 2023 Better Solutions Limited TopNext