Download presentation
Presentation is loading. Please wait.
1
Introduction to Computing Using Java
Array and Table a Michael Fung, CS&E, The Chinese University of HK
2
Array If we need 3 integer variables, we type:
int i, j, k; If we need 1000 integer variables…?! we want to use an index to access variables of the same name (identifier) and type: i[0] = 5; i[654] = -378; How to do that? int[] i = new int[1000]; OR ( C++ style ) int i[] = new int[1000]; integer array type (a collection of integers) a Michael Fung, CS&E, The Chinese University of HK
3
Array If we need 3 integer variables, we type:
int i, j, k; If we need 1000 integer variables…?! we want to use an index to access variables of the same name (identifier) and type: i[0] = 5; i[654] = -378; How to do that? int[] i = new int[1000]; OR int i[] = new int[1000]; create an array of 1000 integer variables a Michael Fung, CS&E, The Chinese University of HK
4
Declaration and Array Creation
int[] i = new int[1000]; is equivalent to int[] i; // i is nothing yet i = new int[1000]; // i keeps something a Michael Fung, CS&E, The Chinese University of HK
5
Revision: Object Creation
Boy i = new Boy(3); is equivalent to Boy i; // i is nothing yet i = new Boy(3); // i keeps something a Michael Fung, CS&E, The Chinese University of HK
6
Another Form of Array Creation
By enumerating its initial values: char[] vowels = {'a', 'e', 'i', 'o', 'u'}; Then vowels is an array of 5 char variables with vowels[0] 'a' vowels[1] 'e' vowels[2] 'i' vowels[3] 'o' vowels[4] 'u' Array index must be an integer: [0 to length – 1]. There are 5 char variables! a Michael Fung, CS&E, The Chinese University of HK
7
Syntax of Creating Arrays
type[] array_name = new type[length]; type[] array_name = {value1, value2, ...}; e.g. double[] GPA = new double[50]; String[] countryCode = new String[175]; String address[] = new String[30]; char[] vowels = {‘a’, ‘e’, ‘i’, ‘o’, ‘u’}; type may be primitive type, class type or even an other array type. a Michael Fung, CS&E, The Chinese University of HK
8
Michael Fung, CS&E, The Chinese University of HK
Use of Array Elements Each array element is a variable by itself: { char[] vowels = {'a', 'e', 'i', 'o', 'u'}; int[] i = new int[1000]; // i[0] to i[999] int j = 4; // serve as array index vowels[0] = 'A'; // simple variable asg’t if (vowels[1] == 'e') System.out.println( vowels[j] ); i[4]++; // increment i[4] by 1 i[j] = i[3] – i[i[4]] * i[999]; } vowels[4] ‘u’ vowels[3] ‘o’ vowels[2] ‘i’ vowels[1] ‘e’ vowels[0] ‘A’ vowels[0] ‘a’ i[4] 1 i[4] a Michael Fung, CS&E, The Chinese University of HK
9
Michael Fung, CS&E, The Chinese University of HK
Properties of Array A bounded (fixed length) and indexed collection of elements of the same type. Array length is fixed at the time of creation. Element access is done using an index [ ]. vowels[0] to vowels[vowels.length – 1] vowels[-8] ArrayIndexOutOfBoundsException To get the length (size) of an array: vowels.length NOT vowel.length() [confused with String] a Michael Fung, CS&E, The Chinese University of HK
10
Example: The Parameter in main()
class TestArgs { public static void main(String[] args) { System.out.println("There are " + args.length + " arguments:"); int i; for (i = 0; i < args.length; i++) System.out.println( args[i] ); } C:\LetSee\> java TestArgs There are 0 arguments: C:\LetSee\> java TestArgs Apple Orange There are 2 arguments: Apple Orange a Michael Fung, CS&E, The Chinese University of HK
11
Example: The Parameter in main()
A Java application program can receive some parameters at start-up. These start-up parameters are called command-line arguments. The main() method is the receiver of such arguments. Such arguments are stored in a String array created by the JVM. JVM sends a message to main() with such an array. a Michael Fung, CS&E, The Chinese University of HK
12
Revision: Difference Between Primitive Types and Object References
/* 8 primitive types: byte, short, int, long, float, double, char, boolean */ int i; char c; Octopus myCard; // object reference Account ansonAccount; // object reference i and c have their own storage spaces for storing the actual data values. myCard and ansonAccount are just references to objects. Without initialization, object references refer to nothing and are said to be null. a Michael Fung, CS&E, The Chinese University of HK
13
Revision: Initializing Object References
int i; // an integer variable i = 45 * 89; // it stores 0 by default Octopus myCard; // a null object reference myCard = new Octopus(); // create a new object and myCard.addValue(100); // let myCard refer to it Octopus yourCard; // a null object reference yourCard = new Octopus(); // create another object yourCard.addValue(50); // it’s your card Octopus stolenCard; // a null object reference stolenCard = myCard; // myCard and stolenCard // now refer to the SAME card! a Michael Fung, CS&E, The Chinese University of HK
14
Michael Fung, CS&E, The Chinese University of HK
Pictorially... value 150.00 String (object) “Michael Fung” String (class) age 21 Octopus (object) ... Octopus (class) name (reference) myCard (reference) double value = ; int age = 21; String name = “Michael Fung”; Octopus myCard = new Octopus(); a Michael Fung, CS&E, The Chinese University of HK
15
Array of Object References
int[] i; // a null integer array reference i = new int[100]; // create a new integer array i[5] = 87; // let i refer to the array // initially, i[0] = … = i[99] = 0 Octopus[] deck; // a null Octopus array reference deck = new Octopus[10]; // initially, deck[0] = … = null deck[0] = new Octopus(); deck[1] = deck[0]; deck[2] = new Octopus(); Creating a new array Creating members a Michael Fung, CS&E, The Chinese University of HK
16
Array Itself is Also a Reference
deck[ ] (reference) ? Octopus[] deck; deck = new Octopus[3]; deck[0] = new Octopus(); deck[1] = deck[0]; deck[2] = new Octopus(); a Michael Fung, CS&E, The Chinese University of HK
17
Array Itself is Also a Reference
deck[ ] (reference) ? ? ? deck[2] (reference) deck[1] (reference) Octopus[] deck; deck = new Octopus[3]; deck[0] = new Octopus(); deck[1] = deck[0]; deck[2] = new Octopus(); deck[0] (reference) a Michael Fung, CS&E, The Chinese University of HK
18
Array Itself is Also a Reference
? ? deck[ ] (reference) Octopus (class) Octopus (object) deck[2] (reference) deck[1] (reference) Octopus[] deck; deck = new Octopus[3]; deck[0] = new Octopus(); deck[1] = deck[0]; deck[2] = new Octopus(); deck[0] (reference) a Michael Fung, CS&E, The Chinese University of HK
19
Array Itself is Also a Reference
deck[ ] (reference) ? Octopus (class) Octopus (object) deck[2] (reference) deck[1] (reference) Octopus[] deck; deck = new Octopus[3]; deck[0] = new Octopus(); deck[1] = deck[0]; deck[2] = new Octopus(); deck[0] (reference) a Michael Fung, CS&E, The Chinese University of HK
20
Array Itself is Also a Reference
deck[ ] (reference) Octopus (object) Octopus (class) Octopus (object) deck[2] (reference) deck[1] (reference) Octopus[] deck; deck = new Octopus[3]; deck[0] = new Octopus(); deck[1] = deck[0]; deck[2] = new Octopus(); deck[0] (reference) Class type array a Michael Fung, CS&E, The Chinese University of HK
21
Array Itself is Also a Reference
(int) 9 i[1] (int) 7 i[0] (int) 7 Primitive type array Class type array int[] i; i = new int[3]; i[0] = 7; i[1] = i[0]; i[2] = 9; a Michael Fung, CS&E, The Chinese University of HK
22
Michael Fung, CS&E, The Chinese University of HK
7-Minute Break a Michael Fung, CS&E, The Chinese University of HK
23
Assignment of the Whole Array
(reference) They refer to the same array! i[2] (int) 9 i[1] (int) 7 i[0] (int) 7 j[ ] (reference) int[] i = new int[3]; int[] j; j = i; // object reference copying j[2] = 9; // i[2] = 9 i[1] = 7; // j[2] = 7 j[0] = 7; // i[0] = 7 a Michael Fung, CS&E, The Chinese University of HK
24
Assignment of the Whole Array
(reference) OR, Create another one! i[2] (int) 9 i[1] (int) 7 j[ ] (reference) i[0] (int) 7 int[] i = new int[3]; int[] j; j = new int[5]; j[4] (int) j[3] (int) j[2] (int) j[1] (int) j[0] (int) a Michael Fung, CS&E, The Chinese University of HK
25
Assignment of the Whole Array
(reference) Remember to Keep It Well! i[2] (int) 9 i[1] (int) 7 j[ ] (reference) i[0] (int) 7 int[] i = new int[3]; int[] j; j = new int[5]; j = i; j[4] (int) j[3] (int) Killed j[2] (int) j[1] (int) j[0] (int) a Michael Fung, CS&E, The Chinese University of HK
26
Revision: Primitive Type Parameter Passing
class Student { public static void studyHard(double newGPA) { newGPA = 4.0; } public static void main(String[] args) double GPA = 1.0; Student.studyHard(GPA); System.out.println(GPA); newGPA 4.0 newGPA 1.0 Start here GPA 1.0 Copy actual parameter to formal parameter when sending message. Change to the formal parameter DOES NOT affect actual parameter! a Michael Fung, CS&E, The Chinese University of HK
27
New Concept: Primitive Type Array Argument Passing
class Student { public static void studyHard(double[] newGPAs) { newGPAs[0] = 4.0; newGPAs[1] = 4.0; } public static void main(String[] args) double[] GPAs = new double[2]; GPAs[0] = 1.0; GPAs[1] = 1.5; Student.studyHard(GPAs); System.out.println(GPAs[0]); System.out.println(GPAs[1]); newGPAs (Reference) Start here GPAs[1] 4.0 GPAs[0] GPAs[1] 1.5 GPAs[0] 1.0 GPAs (Reference) Copy array reference to formal parameter when sending message. Change to the formal parameter DOES affect the actual parameter! a Michael Fung, CS&E, The Chinese University of HK
28
Revision: Object Type Argument Passing
Employee (class) class CUHK { public static void fire(Employee victim) { victim.salary = 0; } public static void main(String[] args) Employee michael = new Employee(500); CUHK.fire(michael); if (michael.salary == 0) System.out.println(“Fired!”); class Employee { public int salary; public Employee(int initialSalary) salary = initialSalary; victim (reference) Start here Employee (object) salary (int) michael (reference) a Michael Fung, CS&E, The Chinese University of HK
29
New Concept: Object Type Array Argument Passing
class CUHK { public static void fire(Employee[] victims) { for (int i = 0; i < victims.length; i++) victims[i].salary = 0; } public static void main(String[] args) Employee[] TAs = new Employee[3]; TAs[0] = new Employee(1000); TAs[1] = new Employee(2000); TAs[2] = new Employee(5000); CUHK.fire(TAs); class Employee { public int salary; public Employee(int initialSalary) salary = initialSalary; victims (reference) Employee (object) salary 1000 Start here TAs[0] (reference) Employee (object) salary 2000 TAs (reference) TAs[1] (reference) Employee (object) salary 5000 TAs[2] (reference) a Michael Fung, CS&E, The Chinese University of HK
30
Michael Fung, CS&E, The Chinese University of HK
What’s the type? double Table (2-Level Array) // There are 176 students, 8 assignments // record their marks in double double[][] mark = new double[176][8]; mark[6][0] = 99.34; // mark: 7th student, Asg1 mark[175][6] = 89.12; // mark: last student, Asg7 double[] singleStudent; singleStudent = mark[175]; // refer to the singleStudent[6] = 45.67; // marks of the last one System.out.println(mark[175][6]); // would print 45.67 Elements of an array could be arrays. Array reference of array references. double[] a Michael Fung, CS&E, The Chinese University of HK
31
Michael Fung, CS&E, The Chinese University of HK
Table Illustrated mark[ ][ ] (reference) mark[2] (reference) mark[1] (reference) mark[0] (reference) mark[2][3] (double) 9.45 mark[1][3] (double) 8.48 mark[0][3] (double) 9.11 mark[2][2] (double) 2.49 mark[1][2] (double) 3.40 mark[0][2] (double) 1.42 Array of Array of double mark[2][1] (double) 3.43 mark[1][1] (double) 6.13 mark[0][1] (double) 5.43 mark[2][0] (double) 1.75 mark[1][0] (double) 1.15 mark[0][0] (double) 0.35 a Michael Fung, CS&E, The Chinese University of HK
32
Duplicating an int Array
(reference) Copy the elements one-by-one i[2] (int) 9 i[1] (int) 7 i[0] (int) 7 int[] i = {7, 7, 9}; int[] j; j = new int[i.length]; for (int count = 0; count < i.length; count++) j[count] = i[count]; a Michael Fung, CS&E, The Chinese University of HK
33
Duplicating an int Array
(reference) Copy the elements one-by-one i[2] (int) 9 i[1] (int) 7 j[ ] (reference) i[0] (int) 7 int[] i = {7, 7, 9}; int[] j; j = new int[i.length]; for (int count = 0; count < i.length; count++) j[count] = i[count]; ? a Michael Fung, CS&E, The Chinese University of HK
34
Duplicating an int Array
(reference) Copy the elements one-by-one i[2] (int) 9 i[1] (int) 7 j[ ] (reference) i[0] (int) 7 int[] i = {7, 7, 9}; int[] j; j = new int[i.length]; for (int count = 0; count < i.length; count++) j[count] = i[count]; j[2] (int) j[1] (int) j[0] (int) a Michael Fung, CS&E, The Chinese University of HK
35
Duplicating an int Array
(reference) Copy the elements one-by-one i[2] (int) 9 i[1] (int) 7 j[ ] (reference) i[0] (int) 7 int[] i = {7, 7, 9}; int[] j; j = new int[i.length]; for (int count = 0; count < i.length; count++) j[count] = i[count]; j[2] (int) 9 j[1] (int) 7 j[0] (int) 7 a Michael Fung, CS&E, The Chinese University of HK
36
Duplicating an Object Array
deck[ ] (reference) Octopus (object) Octopus (class) Octopus (object) deck[2] (reference) deck[1] (reference) Octopus[] deck; ... Octopus[] newDeck; newDeck = new Octopus[deck.length]; for (int count = 0; count < deck.length; count++) newDeck[count] = deck[count]; deck[0] (reference) a Michael Fung, CS&E, The Chinese University of HK
37
Duplicating an Object Array
newDeck[ ] (reference) deck[ ] (reference) Octopus (object) Octopus (class) Octopus (object) ? deck[2] (reference) deck[1] (reference) Octopus[] deck; ... Octopus[] newDeck; newDeck = new Octopus[deck.length]; for (int count = 0; count < deck.length; count++) newDeck[count] = deck[count]; deck[0] (reference) a Michael Fung, CS&E, The Chinese University of HK
38
Duplicating an Object Array
newDeck[ ] (reference) deck[ ] (reference) Octopus (object) Octopus (class) Octopus (object) newDeck[2] (reference) deck[2] (reference) newDeck[1] (reference) deck[1] (reference) Octopus[] deck; ... Octopus[] newDeck; newDeck = new Octopus[deck.length]; for (int count = 0; count < deck.length; count++) newDeck[count] = deck[count]; newDeck[0] (reference) deck[0] (reference) a Michael Fung, CS&E, The Chinese University of HK
39
Duplicating an Object Array
newDeck[ ] (reference) deck[ ] (reference) Octopus (object) Octopus (class) Octopus (object) Only the object references are copied! newDeck[2] (reference) deck[2] (reference) newDeck[1] (reference) deck[1] (reference) Octopus[] deck; ... Octopus[] newDeck; newDeck = new Octopus[deck.length]; for (int count = 0; count < deck.length; count++) newDeck[count] = deck[count]; newDeck[0] (reference) deck[0] (reference) a Michael Fung, CS&E, The Chinese University of HK
40
Michael Fung, CS&E, The Chinese University of HK
End Note Readings and References Sections 7.1, 7.2, 7.3, 7.4, 7.6 Exercises 7.1, 7.2, 7.3, 7.4, 7.5 Programming Projects 7.1, 7.2, 7.3, 7.5, 7.6 a Michael Fung, CS&E, The Chinese University of HK
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.