Islamic University of Gaza Faculty of Engineering Computer Engineering Department Computer Programming Lab (ECOM 2114) Created by Eng: Mohammed Alokshiya Modified by Eng: Mohammed Abdualal Lab 9 Arrays Eng. Mohammed Abdualal
November 30, 2014 An array is an ordered collection of elements of the same type, identified by a pair of square brackets [ ]. Imagine the situation that you need to store 20 names of students as strings and 20 integers as marks, then you need to define 40 variables, and this is clearly very hard and not practical, in such case you need to use arrays. Arrays are indexed data types, that means they are storing different elements discriminating between them using unique index for each one. Figure (1) shows the structure of an array. Figure (1) As Figure (1) shows, the size of an array is fixed, we will refer to array maximum size as array length, it is also clear that indices of an array are zero-based, that is, they start from 0 to length 1. The syntax of declaring Array in Java is as follow: Array s Declaration Syntax DataType [] arrayname; To initialize the array, use new keyword and specify the length. Initializing an Array DataType [] arrayname = new DataType[arrayLength]; Examples: Examples int [] marks = new int[5]; // array of 5 integers double [] values = new double[12]; // array of 12 doubles boolean [] primes = new boolean[2]; // array of 2 booleans String [] names = new String[50]; // array of 50 strings 2
When you initialize an array using new keyword, all elements will take the default value of the array data type. For example, all elements of marks array will be 0, all values of primes array will be false, and all values of names array will be null. Note that you can write the square brackets after the array name, like this: Array s Declaration Syntax int marks[]; However, this is not a recommended syntax. To access array elements, use array name with desired index specified between square brackets [ ]. Accessing Array Elements int [] array = new int[5]; array[0] = 0; array[1] = 1; array[2] = 4; array[3] = 9; array[4] = 16; You can use loops to access array elements, for example printing them to the console. Printing Array Elements for (int i = 0; i < 5; i++) { System.out.println(array[i]); Array Initializers Java has a shorthand notation, known as the array initializer, which combines the declaration, creation, and initialization of an array in one statement using the following syntax: Array s Initializer Syntax elementtype[] arrayrefvar = {value0, value1,..., valuek; Example: Array Initializers double[] mylist = {1.9, 2.9, 3.4, 3.5; 3
The previous statement declares, creates, and initializes the array mylist with four elements, which is equivalent to the following statements: Equivalent Statements double[] mylist = new double[4]; mylist[0] = 1.9; mylist[1] = 2.9; mylist[2] = 3.4; mylist[3] = 3.5; Example: write a program to read 10 integers, and print them in reverse order. Code import java.util.scanner; public class Main { public static void main(string[] args) { Scanner input = new Scanner(System.in); int [] arr = new int[10]; for (int i = 0; i < 10; i++) { arr[i] = input.nextint(); for (int i = 9; i >= 0; i--) { System.out.println(arr[i]); Passing Arrays to Methods When passing an array to a method, the reference of the array is passed to the method. Example: write a method sum that take an array of integers as input, and return the sum of all integers in it. sum Method public static int sum (int [] array) { int s = 0; for (int i = 0; i < array.length; i++) { s += array[i]; return s; 4
You call the previous method by passing the array name into it: Calling sum Method public static void main(string[] args) { int [] arr = {1, 2, 3, 4, 5; int s = sum(arr); System.out.println(s); Multi-Dimensional Array Multi-Dimensional array is an array of arrays. For example, to declare two-dimensional array, use the following syntax: Two-Dimensional Array Declaration Syntax DataType [][] matrix; To initialize the matrix, use the new keyword. Example: create a new matrix with 5 rows and 5 columns: Initializing Two-Dimensional Array int [][] matrix = new int[5][5]; The previous code will create the matrix and fill it with the default value: 0. The matrix will be represented in memory as array of arrays, as following: 5
For example, suppose x = new int[3][4], x[0], x[1], and x[2] are one-dimensional arrays and each contains four elements, as shown in the next figure. x.length is 3, and x[0].length, x[1].length, and x[2].length are 4. 6