Download as pdf or txt
Download as pdf or txt
You are on page 1of 4

IT1708

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.

Figure 1. Array num (Malik, 2012)


Like any other primitive data type variable, an array can also be initialized with specific values when it is declared. For example, the
following Java statements declare an array variable named prices of double type with five (5) elements and initialize these elements
to specific values:
double prices[] = {12.25, 32.50, 16.90, 1.25, 43};
The initializer list contains values, called initial values, that are placed between braces and separated by commas. The elements are
accessed as follows:

Figure 2. One-dimensional array prices


The following is an example of different ways to declare an array and how to assign and access each element:
public class ArrayExample {
public static void main(String[] args) {
int[] ageList = new int[5];
ageList[0] = 23; //initialize the array elements using assignment operator
ageList[1] = 18;
ageList[2] = 22;
ageList[3] = 17;
ageList[4] = 34;
double prices[] = {12.25, 32.50, 16.90, 1.25, 43}; //initial values
int index = 3;
System.out.println(prices[index + 1]); //returns 43.0
}
}
A one-dimensional array is an array in which all the elements are arranged in a list form. The general form to declare a one-dimensional
array is: dataType[] arrayName = new dataType[arraySize];. Some basic operations performed on a one-dimensional array
are initializing the array, reading data into the array, and storing output data in the array. If the data type of an array is numeric, some
common operations are to find the sum and of the elements of the array. Each of these operations requires the ability to step through
the elements of the array, which is easily accomplished by using a loop.
When processing array elements, the for loop or foreach loop is used because all the elements in an array are of the same type, and
the size of the array is known. For example, the Java statements below will print all the elements of the array prices using for loop:
double prices[] = {12.25, 32.50, 16.90, 1.25, 43};
for(int i = 0; i < prices.length; i++) { //the length method is used to count the size of an array
System.out.println(prices[i]);
}
The foreach loop or enhanced for loop enables the user to traverse the complete array sequentially without using an index variable.
For example:
for (double value: prices) {
System.out.println(value);
}

10 Handout 1 *Property of STI


Page 1 of 4
IT1708

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;

Figure 3. Two-dimensional array scores


Two-dimensional arrays can also be initialized when they are declared. To initialize a two-dimensional array when it is declared, the
elements of each row are enclosed within braces and separated by commas, and all rows are enclosed within braces. The following
example statements declare a two-dimensional array named board with 4 rows and 3 columns:
int[][] board = {
{2, 3, 1},
{15, 25, 13},
{20, 4, 7},
{11, 18, 14}
};
System.out.println(board.length); //return the number of
rows of the array: 4
System.out.println(board[0].length); //return the number
of columns in a row: 3 Figure 4. Two-dimensional array board

A two-dimensional array can be processed in three (3) common ways:


1. Process the entire array.
2. Process a particular row of the array, called row processing.
3. Process a particular column of the array, called column processing.
Consider the following declarations:
final int ROWS = 7, COLUMNS = 6;
int[][] matrix = new int[ROWS][COLUMNS];
int sum = 0;
The following for loops process (row processing) all the elements of the row index 5 of the array, matrix:
for (int col = 0; col < matrix[5].length; col++) {
matrix[5][col] = col * 2;
}
for (int col = 0; col < matrix[5].length; col++) {
System.out.print(matrix[5][col] + “\t”); //display the elements of row index 5
}

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
}

10 Handout 1 *Property of STI


Page 2 of 4
IT1708

Consider the following declarations:


int[][] table = {
{ 3, 4, 2, 5, 12 },
{ 8, 11, 23, 2, 3 },
{ 20, 1, 19, 7, 6 },
{ 15, 10, 6, 17, 3 }
};
The following example processes the rows and columns of the array table using a nested for loop:
System.out.println(“Elements of array table:”);
for (int row = 0; row < table.length; row++) {
for (int col = 0; col < table[row].length; col++) {
System.out.print(table[row][col] + "\t");
} System.out.println();
}
Sample Output:
Elements of 2D array table:
3 4 2 5 12
8 11 23 2 3
20 1 19 7 6
15 10 6 17 3

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

10 Handout 1 *Property of STI


Page 3 of 4
IT1708

The Arrays Class


Java SE provides several methods for performing array manipulations in the Arrays class from the java.util package. This class contains
a large number of static help methods for sorting and searching arrays, comparing arrays, and filling array elements. These methods are
overloaded for all primitive types.
The methods in the Arrays class are static methods, which means they are used with the class name without instantiating an Arrays
object. The following are some methods available in Arrays class:

Consider the following declaration:


int[] a = {1, 3, 4, 5, 7, 4};
int[] b = {6, 7, 12, 5, 1, 2};
int[] c = {1, 3, 4, 5, 7, 4};
Method Purpose Example
Searches the specified array for the int index =
specified key value using the binary search Arrays.binarySearch(a, 4);
int binarySearch(type[] a, type algorithm. This returns the index of the //returns index 2
key) search key if it is contained in the list, int index =
otherwise, (- (insertion point) - 1). Arrays.binarySearch(a, 53);
//returns index -7
Returns true if the two (2) specified arrays boolean flag =
of the same type are equal to one (1) Arrays.equals(a, b); //returns
boolean equals(type[] a, type another false
[] b) boolean flag =
Arrays.equals(a, c); //returns
true
Assigns the specified value to each element Arrays.fill(a, 8); //change
of the specified array into ascending order all value of the array to 8
void fill(type[] a, type val) for(int i: a) {
System.out.println(i);
}
Sorts the specified array into ascending Arrays.sort(b); //sort the
void sort(type[] a)
order elements from 1 to 12
void sort(type[] a, int Sorts the specified range of the specified Arrays.sort(b, 1, 5);
fromIndex, int toIndex) array into ascending order
Table 1. Some Methods in the Arrays Class (Farrell, 2014)

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.

10 Handout 1 *Property of STI


Page 4 of 4

You might also like