Professional Documents
Culture Documents
10 Arrays (Final)
10 Arrays (Final)
One-Dimensional Array
An array is a collection or sequence of a fixed number of variables called elements, wherein all the elements are of the same data type.
For example, you might use an array to store a list of values of type int that records a series of numbers. The length of an array is
established when the array is created. After creation, its length is fixed. Each element in an array is accessed by its numerical index.
Indices start at 0. The general form to declare and create an array is:
dataType[] arrayName = new dataType[arraySize];
For example:
int[] num = new int[5];
The example declares and creates the array num consisting of five (5) elements. Each element is of type int. The elements are accessed
as follows: num[0], num[1], num[2], num[3], and num[4]. When an array is created, Java automatically initializes the elements of
numeric arrays as initialized to 0.
Two-Dimensional Array
A two-dimensional array is a collection of a fixed number of elements arranged in rows and columns, wherein all the elements are of the
same data type. The general syntax of the two-dimensional array is:
dataType[][] arrayName = new dataType[rowSize][columnSize];
For example, the following statement declares a two-dimensional array named scores with 4 rows and 5 columns, where every element
is of int type initialized with the default value of 0:
int[][] scores = new int[4][5];
The following statement stores a value of 43 into row number 2 and column number 3 of the array, scores:
scores [2][3] = 43;
The following for loops process (column processing) column index 2 of the array, matrix:
for (int row = 0; row < matrix.length; row++) {
matrix[row][2] = row * 3;
}
for (int row = 0; row < matrix.length; row++) {
sum = sum + matrix[row][2]; //sum up all the elements of column index 2
}
Multidimensional Array
Multidimensional arrays are arrays where the elements are arrays themselves. The general syntax for declaring and instantiating a
multidimensional array is:
dataType[][]…[] arrayName = new dataType[exp1][exp2]…[n];
For example:
int[][][] numbers = new int[2][3][5];
The example declares an array named numbers with 2 groups of elements, where each group has 3 lists of elements. Each list contains
5 elements. As a result, the array contains 30 elements. This can also be initialized when declared, for example:
int[][][] numbers = {
{
{1, 3, 5, 7, 9},
{11, 13, 15, 17, 19},
{21, 23, 25, 27, 29}
},
{
{2, 4, 6, 8, 10},
{12, 14, 16, 18, 20},
{22, 24, 26, 28, 30}
}
};
System.out.println(numbers[1][2][3]); //returns 28
A multidimensional array can also be initialized with another declared array, for example:
int[][] odd = {
{1, 3, 5, 7, 9},
{11, 13, 15, 17, 19},
{21, 23, 25, 27, 29}
};
int[][] even = {
{2, 4, 6, 8, 10},
{12, 14, 16, 18, 20},
{22, 24, 26, 28, 30}
};
int[][][] numbers = {odd, even};
System.out.println(numbers.length); //returns the number of group elements: 2
System.out.println(numbers[0].length); //returns the number of list in a group: 3
System.out.println(numbers[0][2].length);//returns the number of elements in a list: 5
REFERENCES:
Baesens, B., Backiel, A., & Broucke, S. (2015). Beginning java programming: The object-oriented approach. Indiana: John
Wiley & Sons, Inc.
Farrell, J. (2014). Java programming, 7th edition. Boston: Course Technology, Cengage Learning.
Malik, D.S. (2012). Java programming: from problem solving to program design, 5th edition. Boston: Course, Technology,
Cengage Learning.
Savitch, W. (2014). Java: An introduction to problem solving and programming, 7th edition. California: Pearson Education,
Inc.