Introducing Arrays: Array Is A Data Structure That Represents A Collection of The Same Data Type of Data
Introducing Arrays: Array Is A Data Structure That Represents A Collection of The Same Data Type of Data
myArray[4] 4
Array element at
myArray[5] 34.33 Element value
index 5
myArray[6] 34
myArray[7] 45.45
myArray[8] 99.993
myArray[9] 11123
Declaring Array Variables
• datatype[] arrayRefVar;
Example:
double[] myArray;
Example:
myArray = new double[10];
arrayRefVar.length
For example,
myArray.length returns 10
Default Values
The array elements are accessed through the index. The array indices
are 0-based, i.e., it starts from 0 to arrayRefVar.length-1. In the
previous example, myArray holds ten double values and the indices
are from 0 to 9.
arrayRefVar[index];
Using Indexed Variables
Using the shorthand notation, you have to declare, create, and initialize the array all
in one statement. Splitting it would cause a syntax error. For example, the
following is wrong:
double[] myArray;
myArray = {1.9, 2.9, 3.4, 3.5};
Trace Program with Arrays
values[i] = i + values[i-1]; 2 0
} 3 0
4 0
values[0] = values[1] + values[4];
}
}
Trace Program with Arrays
i becomes 1
values[i] = i + values[i-1]; 1 0
2 0
}
3 0
values[0] = values[1] + values[4]; 0
4
}
}
Trace Program with Arrays
} 2 0
3 0
values[0] = values[1] + values[4];
4 0
}
}
Trace Program with Arrays
values[i] = i + values[i-1]; 2 0
} 3 0
0
values[0] = values[1] + values[4]; 4
}
}
Trace Program with Arrays
1 1
}
2 0
values[0] = values[1] + values[4]; 0
3
} 4 0
}
Trace Program with Arrays
i (= 2) is less than 5
public class Test {
public static void main(String[] args) {
int[] values = new int[5];
After the first iteration
for (int i = 1; i < 5; i++) {
values[i] = i + values[i-1];
0 0
} 1
1
values[0] = values[1] + values[4]; 2 0
} 3 0
} 4 0
Trace Program with Arrays
values[i] = i + values[i-1]; 2 3
} 3 0
4 0
values[0] = values[1] + values[4];
}
}
Trace Program with Arrays
values[i] = i + values[i-1]; 2 3
} 3 0
4 0
values[0] = values[1] + values[4];
}
}
Trace Program with Arrays
values[i] = i + values[i-1]; 2 3
3 0
}
4 0
values[0] = values[1] + values[4];
}
}
Trace Program with Arrays
After this line, values[3] becomes 6 (3 + 3)
values[i] = i + values[i-1]; 2 3
3 6
}
4 0
values[0] = values[1] + values[4];
}
}
Trace Program with Arrays
values[i] = i + values[i-1]; 2 3
} 3 6
0
values[0] = values[1] + values[4]; 4
}
}
Trace Program with Arrays
i (=4) is still less than 5
values[i] = i + values[i-1]; 2 3
} 3 6
0
values[0] = values[1] + values[4]; 4
}
}
Trace Program with Arrays
values[i] = i + values[i-1]; 2 3
} 3 6
10
values[0] = values[1] + values[4]; 4
}
}
Trace Program with Arrays
values[i] = i + values[i-1];
0 0
} 1 1
3 6
} 4 10
}
Trace Program with Arrays
3 6
} 4 10
}
Trace Program with Arrays
values[i] = i + values[i-1]; 1 1
} 2 3
6
values[0] = values[1] + values[4]; 3
10
} 4
}
Processing Arrays
• Objective: The program receives 6 numbers from the user, finds the
largest number and counts the occurrence of the largest number entered.
Suppose you entered 3, 5, 2, 5, 5, and 5, the largest number is 5 and its
occurrence count is 4.
Example: Assigning Grades
• Objective: read student scores (int, 0-100), get the best score,
and then assign grades based on the following scheme (store
grades in another array):
–Grade is A if score is >= best–10;
–Grade is B if score is >= best–20;
–Grade is C if score is >= best–30;
–Grade is D if score is >= best–40;
–Grade is F otherwise.
Copying Arrays
array1 array1
Contents Contents
of array1 of array1
array2 array2
Contents Contents
of array2 of array2
Garbage
Copying Arrays
array1 array1
Contents Contents
of array1 of array1
array2 array2
Contents Contents
of array2 of array2
Garbage
Copying Arrays (the values within the array)
Using a loop:
int[] sourceArray = {2, 3, 1, 5, 10};
int[] targetArray = new int[sourceArray.length];
Example:
System.arraycopy(sourceArray, 0, targetArray, 0,
sourceArray.length);
Anonymous Array
The statement
printArray(new int[]{3, 1, 2, 6, 4, 2});
Java uses pass by value to pass parameters to a method. There are important
differences between passing a value of variables of primitive data types and
passing arrays.
Stack Heap
Space required for
method m
int[] numbers:reference
The arrays are
int number: 1 Array of stored in a
ten int heap.
Space required for the values is
main method stored here
int[] y: reference
int x: 1
Stack Heap
Space required for
xMethod
int[] numbers:reference
The arrays are
int number: 1 Array of stored in a
ten int heap.
Space required for the values are
main method stored here
int[] y: reference
int x: 1
return result;
}
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 0 0
Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 0 and j = 5
public static int[] reverse(int[] list) {
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 0 0
Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i (= 0) is less than 6
public static int[] reverse(int[] list) {
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 0 0
Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 0 and j = 5
public static int[] reverse(int[] list) { Assign list[0] to result[5]
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 0 1
Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 0 1
Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 0 1
Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 1 and j = 4
public static int[] reverse(int[] list) { Assign list[1] to result[4]
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 2 1
Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 2 1
Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
return result;
}
list 1 2 3 4 5 6
result 0 0 0 0 2 1
Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 2 and j = 3
public static int[] reverse(int[] list) { Assign list[i] to result[j]
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 0 0 0 3 2 1
Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
return result;
}
list 1 2 3 4 5 6
result 0 0 0 3 2 1
Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
return result;
}
list 1 2 3 4 5 6
result 0 0 0 3 2 1
Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 3 and j = 2
public static int[] reverse(int[] list) { Assign list[i] to result[j]
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 0 0 4 3 2 1
Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
return result;
}
list 1 2 3 4 5 6
result 0 0 4 3 2 1
Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
return result;
}
list 1 2 3 4 5 6
result 0 0 4 3 2 1
Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 4 and j = 1
public static int[] reverse(int[] list) { Assign list[i] to result[j]
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 0 5 4 3 2 1
Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
return result;
}
list 1 2 3 4 5 6
result 0 5 4 3 2 1
Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
return result;
}
list 1 2 3 4 5 6
result 0 5 4 3 2 1
Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
i = 5 and j = 0
public static int[] reverse(int[] list) { Assign list[i] to result[j]
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
result 6 5 4 3 2 1
Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
return result;
}
list 1 2 3 4 5 6
result 6 5 4 3 2 1
Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
return result;
}
list 1 2 3 4 5 6
result 6 5 4 3 2 1
Trace the reverse Method, cont.
int[] list1 = new int[]{1, 2, 3, 4, 5, 6};
int[] list2 = reverse(list1);
Return result
public static int[] reverse(int[] list) {
int[] result = new int[list.length];
return result;
}
list 1 2 3 4 5 6
list2
result 6 5 4 3 2 1
Example: Counting Occurrence of Each Letter
// Alternative syntax
dataType refVar[][] = new dataType[10][10];
Declaring Variables of Two-dimensional Arrays
and Creating Two-dimensional Arrays
double[][] x;
Two-dimensional Array Illustration
0 1 2 3 4 0 1 2 3 4 0 1 2
0 0 0 1 2 3
1 1 1 4 5 6
2 2 7 2 7 8 9
3 3 3 10 11 12
4 4 int[][] array = {
{1, 2, 3},
matrix = new int[5][5]; matrix[2][1] = 7; {4, 5, 6},
{7, 8, 9},
{10, 11, 12}
};
matrix.length? 5 array.length? 4
matrix[0].length? 5 array[0].length? 3
Declaring, Creating, and Initializing
using Shorthand Notations
You can also use an array initializer to declare, create and initialize a two-dimensional array.
For example,
int[][] array = {
int[][] array = new int[4][3];
{1, 2, 3}, array[0][0] = 1; array[0][1] = 2; array[0][2] = 3;
{4, 5, 6}, array[1][0] = 4; array[1][1] = 5; array[1][2] = 6;
Same as
{7, 8, 9}, array[2][0] = 7; array[2][1] = 8; array[2][2] = 9;
{10, 11, 12} array[3][0] = 10; array[3][1] = 11; array[3][2] = 12;
};
Lengths of Two-dimensional Arrays
x
x[0][0] x[0][1] x[0][2] x[0][3] x[0].length is 4
x[0]
x[1] x[1][0] x[1][1] x[1][2] x[1][3] x[1].length is 4
x[2]
x[2][0] x[2][1] x[2][2] x[2][3] x[2].length is 4
x.length is 3
Lengths of Two-dimensional Arrays, cont.
int[][] array = {
array.length
{1, 2, 3}, array[0].length
{4, 5, 6}, array[1].length
{7, 8, 9}, array[2].length
array[3].length
{10, 11, 12}
};
array[4].length ArrayIndexOutOfBoundsException
Example: Grading Multiple-Choice Test
• Ragged arrays
• Computing Taxes Using Arrays
Ragged Arrays (Optional)
Each row in a two-dimensional array is itself an array. So, the rows can have different
lengths. Such an array is known as a ragged array. For example,
int[][] matrix = {
{1, 2, 3, 4, 5},
{2, 3, 4, 5},
{3, 4, 5},
matrix.length is 5
{4, 5},
matrix[0].length is 5
{5} matrix[1].length is 4
}; matrix[2].length is 3
matrix[3].length is 2
matrix[4].length is 1
Ragged Arrays, cont.
int[][] triangleArray = { 1 2 3 4 5
{1, 2, 3, 4, 5},
{2, 3, 4, 5}, 1 2 3 4
{3, 4, 5},
{4, 5}, 1 2 3
{5}
}; 1 2
1 2
Example: Computing Taxes Using Arrays
Rotate
• Objective: write a program that calculates the total score for students in a class. Suppose
the scores are stored in a three-dimensional array named scores. The first index in scores
refers to a student, the second refers to an exam, and the third refers to the part of the exam.
Suppose there are 7 students, 5 exams, and each exam has two parts--the multiple-choice
part and the programming part. So, scores[i][j][0] represents the score on the multiple-
choice part for the i’s student on the j’s exam. Your program displays the total score for
each student.