Chapter 9: Arrays, Strings and Vectors
9.1 Arrays
An array is a group of contiguous or related data items that share a common name.
A particular value is indicated by writing a number called index number or subscript in
brackets after the array name.
Eg: salary[10]
The complete set of values is referred to as an array, the individual values are called
elements.
Arrays can be of any variable type.
The ability to use a single name to represent a collection of items and to refer to an item
by specifying the item number enables to develop concise and efficient programs.
9.2 One-Dimensional Arrays
A list of items can be given one variable name using only one subscript and such a
variable is called a single-subscripted variable or a one-dimensional array.
9.3 Creating an Array
Creation of an array involves three steps:
1. Declare the array
o Arrays can be declared in 2 forms:
1. type arrayname[];
2. type[] arrayname;
Eg:
int number[];
int[] counter;
2. Create memory locations
o Allows us to create arrays using new operator
Arrayname = new type[size];
Eg:
Number = new int[5];
These lines create necessary memory locations for the arrays number and designate as int.
3. Put values into the memory locations
o The final step is to put values into the array created. This process is called
initialization.
o This can be achieved using the array subscripts:
Arrayname[subscript] = value;
Eg: array[0] = 45
o We can also initialize arrays automatically in the same way as the ordinary
variables when they are declared.
type arrayname[] = {list of values};
Eg: int number[] = {23,34,45,67};
Array length
o All arrays store the allocated size in a variable named length.
o We can access the length of the array ‘a’ using a.length
Eg: int aSize = a.length
9.4 Two-Dimensional Arrays
For creating 2-dimensional arrays:
int myArray[][];
myArray = new int[3][4];
9.1 Arrays
An array is a group of contiguous or related data items that share a common name.
A particular value is indicated by writing a number called index number or subscript in
brackets after the array name.
Eg: salary[10]
The complete set of values is referred to as an array, the individual values are called
elements.
Arrays can be of any variable type.
The ability to use a single name to represent a collection of items and to refer to an item
by specifying the item number enables to develop concise and efficient programs.
9.2 One-Dimensional Arrays
A list of items can be given one variable name using only one subscript and such a
variable is called a single-subscripted variable or a one-dimensional array.
9.3 Creating an Array
Creation of an array involves three steps:
1. Declare the array
o Arrays can be declared in 2 forms:
1. type arrayname[];
2. type[] arrayname;
Eg:
int number[];
int[] counter;
2. Create memory locations
o Allows us to create arrays using new operator
Arrayname = new type[size];
Eg:
Number = new int[5];
These lines create necessary memory locations for the arrays number and designate as int.
3. Put values into the memory locations
o The final step is to put values into the array created. This process is called
initialization.
o This can be achieved using the array subscripts:
Arrayname[subscript] = value;
Eg: array[0] = 45
o We can also initialize arrays automatically in the same way as the ordinary
variables when they are declared.
type arrayname[] = {list of values};
Eg: int number[] = {23,34,45,67};
Array length
o All arrays store the allocated size in a variable named length.
o We can access the length of the array ‘a’ using a.length
Eg: int aSize = a.length
9.4 Two-Dimensional Arrays
For creating 2-dimensional arrays:
int myArray[][];
myArray = new int[3][4];