Full Stack
🗙
Software Engineering
🗙
Java Arrays
By Max Wong
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
2
What will you learn?
• Understand what is an array
• How to initiate an array
• How to iterate an array
• Understand multi-dimensional array
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
Elementary Array
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
4
Native Approach
import [Link].*;
public class Naive { • This program works if
public static void main(String [] args) { there are only four
Scanner keyboard = new Scanner([Link]); students
int grade1, grade2, grade3, grade4;
double average;
grade1 = [Link](); • What if the size is 100?
grade2 = [Link](); Do we have to declare
grade3 = [Link]();
grade4 = [Link](); 100 variables?
average = (grade1 + grade2 + grade3 + grade4) / 4.0;
[Link]("Average = " + average);
}
}
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
5
What is an array?
Variable Array
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
6
array index
What is an array?
• An array is a sequenced collection of elements of grade[0] 89
the same data type. grade[1] 76
grade[2] 54
• One single name
grade[3] 32
• Indexable
grade[4] 91
• Stored contiguously
grade[5] 65
• The grade array can hold 100 distinct integer values simultaneously.
• The array elements are referred to as grade[0], grade[1], ..., ...
grade[99].
• The integer value inside [ ] is called the array subscript or index. grade[97] 65
• Note carefully that array subscript starts with 0 in Java. grade[98] 65
• All the elements are of the same type, int in this example. grade[99] 65
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
7
Declare and Use an Array
All values stored in the array elements are
• An array is an object initialized with that type’s own default value.
As an object, an array can be referred to by a reference
variable (myArray in this example).
• int[] myArray
Declares a reference variable which can be used to refer. myArray[0] = 0
• new int[10] myArray[1] = 0
Creates an int array object of 10 elements (indexed 0 to 9). myArray[2] = 0
myArray[3] = 0
public class DeclareArray { myArray[4] = 0
public static void main(String [] args) myArray[5] = 0
{ myArray[6] = 0
Data Type int[] myArray = new int[10]; Create an int array object myArray[7] = 0
containing 10 elements myArray[8] = 0
for (int i = 0; i < 10; i++) myArray[9] = 0
[Link]("myArray["+i+"]=" + myArray[i]);
} To access a particular array element, use the array
} reference, followed by a pair of brackets, which
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab enclose the index of the element to be accessed.
8
Declare and Use an Array
Default Value
myArray[0] 0
myArray[1] 0
myArray[2] 0
myArray[3] 0
myArray[4] 0
int[] myArray; int[] myArray = new int[5];
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
9
Array Bounds
public class DeclareArray {
myArray[0] = 0 public static void main(String [] args)
myArray[1] = 0 {
myArray[2] = 0 int[] myArray = new int[10];
myArray[3] = 0
myArray[4] = 0 for (int i = 0; i <= 10; i++)
myArray[5] = 0 [Link]("myArray["+i+"]=" + myArray[i]);
myArray[6] = 0 }
myArray[7] = 0 }
myArray[8] = 0
myArray[9] = 0
Exception in thread “main” [Link]: 10
…
Beware!!!! In Java, The first array index is 0. Therefore, the last
index is (number of items – 1).
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
10
Array Property - length
Each array object has the length property, which stores the number of elements in
the array
public class ArrayLength {
public static void main(String [] args) {
In this case, the condition
int[] myArray = new int[10];
statement of the loop has
no need to hard code a
for (int i=0; i < [Link] ; i++) number. Therefore, this for-
[Link]( “myArray[”+ i +"]=" + myArray[i] ); loop is suitable to iterate
} through any arrays.
}
As length is a public instance variable, not a method,
it is no need to add a pair of () when access.
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
11
Example
public class ArrayIterAssignExample {
public static void main(String [] args)
{ Values of array elements can
int[] myArray = new int[10]; also be set with the
for (int i=0; i < [Link]; i++) assignment operator (=).
myArray[i] = i*2;
myArray[0] = 0
for (int i=0; i < [Link]; i++) myArray[1] = 2
[Link]( “myArray[”+ i +"]=" + myArray[i] ); myArray[2] = 4
} myArray[3] = 6
} myArray[4] = 8
myArray[5] = 10
myArray[6] = 12
myArray[7] = 14
myArray[8] = 16
myArray[9] = 18
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
12
Example import [Link].*;
public class Example {
public static void main(String [] args) {
Scanner keyboard = new Scanner([Link]);
Input number: 10 int sum=0;
Input number: 20 int [] myArray = new int [5];
Input number: 30
Input number: 40 for (int i=0; i < [Link]; i++) {
Input number: 50 [Link](“Input number: ");
myArray[i] = [Link]();
Stored: }
myArray[0] = 10
myArray[1] = 20 [Link](“\nStored:”);
myArray[2] = 30 for (int i=0; i < [Link]; i++) {
myArray[3] = 40 [Link]( “myArray[”+ i +"]=" + myArray[i] );
myArray[4] = 50 sum += myArray[i];
}
Sum=150
[Link]();
[Link]("Sum=" + sum);
}
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
}
13
Array Initialization
• The array elements can be initialized with some initializers.
• When an array is declared without specifying the size, the array is implicitly given
the size of the number of initializers.
public class ArrayInitialization {
public static void main(String [] args) {
myArray[0] = 13
int[] myArray = { 13, 56, 2, 90, 100 };
myArray[1] = 56
myArray[2] = 2
for (int i=0; i < [Link]; i++)
myArray[3] = 90
[Link]( “myArray[”+ i +"]=" + myArray[i]);
myArray[4] = 100
}
}
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
Arrays and Methods
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
Recall: From note Methods
15
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
Recall: From note Computer Basics
16
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
17
Pass-by-Value for Objects
All variables in Object (includes public class PassByValueObjectReference {
public static void main(String [] args) {
arrays) duplicate its object int [] a = { 1, 2, 3, 4, 5 };
reference to the called method [Link]("Original Array:");
Original Array: for (int i=0; i < [Link]; i++)
a[0] = 1 [Link]("a[" + i + "]=" + a[i]);
a[1] = 2
modifyArray(a);
a[2] = 3
a[3] = 4 [Link]("\nAfter calling modifyArray():");
a[4] = 5 for (int i=0; i < [Link]; i++)
[Link]("a[" + i + "]=" + a[i]);
After calling modifyArray(): }
a[0] = 2
a[1] = 4 public static void modifyArray( int [] b ) {
for (int j=0; j < [Link]; j++)
a[2] = 6
b[j] *= 2;
a[3] = 8
}
a[4] = 10 }
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
18
Pass-by-Value of Primitive Type in Steps
3
Duplicate
100
Memory 10 10
int a int b
in method in method
main square
1 2
public class PassByValuePrimitiveType {
public static void main(String[] args) {
1 int a = 10;
square(a);
[Link]("number in method main: " + a);
}
public static void square(int b) { 2
3 b *= b;
[Link]("number in method square: " + number);
}
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab }
19
Pass-by-Value of Object Reference in Steps
Array index [0] [1] [2] [3] [4]
Address 0x1200 0x1201 0x1202 0x1203 0x1204 0x1205 0x1206 0x1207 0x1208 0x1209 0x1210 0x1211
1 2 3 2 4 6 8 10
Memory 0x1205 0x1205 1 2 3 4 5
(Duplicated the pointer)
int[] a int[] b public class PassByValueObjectReference {
in method in method public static void main(String [] args) {
main modify int[] a = { 1, 2, 3, 4, 5 };
1
modify(a);
[Link]("\nAfter calling modify():");
for (int i=0; i < [Link]; i++)
[Link]("a[" + i + "]=" + a[i]);
}
public static void modify( int [] b ) { 2
for (int j=0; j < [Link]; j++)
b[j] *= 2;
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab } 3
}
20
Passing Variable to Method Summary
The process is named as Pass-by-Value.
Primitive Data Types: (boolean, byte, short, int, long, float, double, char)
The original value is stored inside the variable memory
Others (Object, including Arrays):
The object reference is stored inside the variable memory
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
Multi-Dimensional Arrays
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
We can imagine an array is a row
How about a table?
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
Do you need to create 99 arrays for
a table with 99 rows?
...
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
24
Creating 2-D Array
public class TwoDArray { b[0][0] = 0
public static void main(String [] args) { b[0][1] = 1
// 2-D array with explicit object creation b[0][2] = 2
int [][] b = new int [3][4]; b[0][3] = 3
b[1][0] = 1
for (int i=0; i < 3; i++) // number of rows b[1][1] = 2
for (int j=0; j < 4; j++) // number of columns b[1][2] = 3
b[i][j] = i + j; b[1][3] = 4
b[2][0] = 2
[Link](); b[2][1] = 3
for (int i=0; i < 3; i++) { b[2][2] = 4
for (int j=0; j < 4; j++) b[2][3] = 5
[Link](“b[”+ i +“][”+ j +"]=" + b[i][j]);
}
} i=0 i=1 i=2 i=3
j=0 b[0][0]=0 b[0][1]=1 b[0][2]=2 b[0][3]=3
}
j=1 b[1][0]=1 b[1][1]=2 b[1][2]=3 b[1][3]=4
j=2 b[2][0]=2 b[2][1]=3 b[2][2]=4 b[2][3]=5
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
25
Creating 2-D Array Implicitly
public class ImplicitTwoDArray {
public static void main(String [] args) {
// 2-D array with initialization b[0][0] = 1
int [][] b = { {1, 2, 3}, {4, 5, 6} }; b[0][1] = 2
b[0][2] = 3
[Link](); b[1][0] = 4
b[1][1] = 5
for (int i=0; i < 2; i++) { b[1][2] = 6
for (int j=0; j < 3; j++)
[Link](“b[”+ i +“][”+ j +"]=", b[i][j]);
}
}
}
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
26
What is a 2-D Array?
public class TwoDArray {
public static void main(String [] args)
{
// 2-D array with explicit object creation
int [][] b = new int [3][4];
...
} b
}
A 2-D array can be regarded as a 1-D array of
references to a 1-D array.
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
27
Lengths of 2-D Arrays
public class 2DArrayLengths {
public static void main(String [] args) { b[i].length = 4
// 2-D array with explicit object creation
[Link] = 3
int [][] b = new int [3][4];
for (int i=0; i < [Link] ; i++) // number of rows
for (int j=0; j < b[i].length; j++) // number of columns
b[i][j] = i + j;
27
[Link]();
[0]
for (int i=0; i < [Link]; i++) {
for (int j=0; j < b[i].length; j++)
b
[Link](“b[”+ i +“][”+ j +"]=" + b[i][j]);
}
}
[1]
}
[2]
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
2-D Array
Simple Data Type Array
3-D Array
N-D Array
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab
30
What did you learn?
• Understand what is an array
• How to initiate an array
• How to iterate an array
• Understand multi-dimensional array
Prepared by Jeffrey Chan and Max Wong in Venturenix Lab