By definition, an array is a list of variables, all with the same data type and name.
When we work with a single item, we only need to use one variable.
However, if we have a list of items which are of similar type to deal with, we need to declare an array of variables instead of using a variable for each item.
We differentiate each item in the array by using subscript, the index value of each item, for example name[0]
, name[1]
, name[2]
, etc.
Declaring Arrays
We declare an array just as the way we declare a single variable.
The general format to declare an array is as follows:
dataType[ ] arrayName = new dataType[subs];
where subs-1
indicates the last subscript in the array.
For example,
string[ ] cusName = new string[10];
will declare an array that consists of 10 elements, starting from cusName[0]
to cusName[9]
.
An Example
The following example accepts data entry through an input box and displays the entries in a list box.