An array is essentially a variable that can contain a list of values. Each item in the array is accessed using an index that corresponds to the order in which the variables were defined in the list.
Arrays can either be single- or multi-dimensional. Single-dimension arrays are used to store a single set of values, while multi-dimension arrays provide a way to store data into rows and columns.
An array must be declared before you can use it. This initially sets the array’s name and type in memory. The type determines what values can be stored in the array ( ex. strings, integers, dates, etc. ). Arrays of a given type can only hold values of that type.
Arrays can be declared in several ways, the simplest of which is to initialize an instance of the array type and at the same time assign values to the array. In these cases, the given values must be separated by commas and enclosed within a pair of symbols that is language-specific. The following shows an example of how you can declare and asign values to a single-dimension array.
// an array of strings
String [ ] strArray = {"Red", "Green", "Blue"};
// an array of integers
int [ ] intArray = {1, 2, 3};
' an array of strings
Dim strArray ( ) As String = {"Red", "Green", "Blue"}
' an array of integers
Dim intArray ( 2 ) As Integer = {1, 2, 3}
// an array of strings
var strArray : String [ ] = [ "Red", "Green", "Blue" ];
// an array of integers
var intArray : int [ ] = [ 1, 2, 3 ]; |
|
C# |
VB |
JScript |
NOTE: When declaring arrays, C# and JScript use a pair of square brackets [ ] after the type identifier, while VB uses a pair of parentheses ( ) after the array name.
After an array is declared, the individual elements of the array can be accessed using the following notation.
strArray [ index ]
strArray ( index )
strArray [ index ] |
|
C# |
VB |
JScript |
NOTE: Arrays are zero-based; that is, when accessing array elements, the array indexes start at zero.
Show me
The following shows another form of declaring both single- and multi-dimension arrays and how their elements can be accessed. In these cases, the array’s dimension ( the number of elements the array will contain ) must be specified at the time the array is declared.
String [ ] strArray = new String [ 3 ];
strArray [ 0 ] = "Red";
strArray [ 1 ] = "Green";
strArray [ 2 ] = "Blue";
// multi-dimension array with three rows and two columns
String [ ] [ ] strArray = new String [ 3 ] [ 2 ];
strArray [ 0 ] [ 0 ] = "Data for row 1 column 1";
strArray [ 1 ] [ 0 ] = "Data for row 2 column 1";
strArray [ 2 ] [ 0 ] = "Data for row 3 column 1";
Dim strArray ( 2 ) As String
strArray ( 0 ) = "Red"
strArray ( 1 ) = "Green"
strArray ( 2 ) = "Blue"
' multi-dimension array with three rows and two columns
Dim strArray ( 2,1 ) As String
strArray ( 0,0 ) = "Data for row 1 column 1"
strArray ( 1,0 ) = "Data for row 2 column 1"
strArray ( 2,0 ) = "Data for row 3 column 1"
var strArray : String [ ] = new String [ 3 ];
strArray [ 0 ] = "Red";
strArray [ 1 ] = "Green";
strArray [ 2 ] = "Blue";
// multi-dimension array with three rows and two columns
var strArray : String [ ] [ ] = new String [ 3 ] [ 2 ];
strArray [ 0 ] [ 0 ] = "Data for row 1 column 1";
strArray [ 1 ] [ 0 ] = "Data for row 2 column 1";
strArray [ 2 ] [ 0 ] = "Data for row 3 column 1"; |
|
C# |
VB |
JScript |
NOTE: In VB, the dimensions declared must be one-less than the actual number of elements.