400 likes | 416 Views
Introduction to Computing Using Java. Array and Table. 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;
E N D
Introduction to Computing Using Java Array and Table Michael Fung, CS&E, The Chinese University of HK
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) Michael Fung, CS&E, The Chinese University of HK
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 Michael Fung, CS&E, The Chinese University of HK
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 Michael Fung, CS&E, The Chinese University of HK
Revision: Object Creation Boy i = new Boy(3); is equivalent to Boy i; // i is nothing yet i = new Boy(3); // i keeps something Michael Fung, CS&E, The Chinese University of HK
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! Michael Fung, CS&E, The Chinese University of HK
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. 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]++; // incrementi[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] 0 i[4] 1 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] Michael Fung, CS&E, The Chinese University of HK
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 Michael Fung, CS&E, The Chinese University of HK
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. Michael Fung, CS&E, The Chinese University of HK
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. Michael Fung, CS&E, The Chinese University of HK
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! Michael Fung, CS&E, The Chinese University of HK
Pictorially... value 150.00 String (object) “Michael Fung” String (class) double value = 150.00; int age = 21; String name = “Michael Fung”; Octopus myCard = new Octopus(); age 21 Octopus (object) ... Octopus (class) name (reference) myCard (reference) Michael Fung, CS&E, The Chinese University of HK
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 Michael Fung, CS&E, The Chinese University of HK
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(); ? Michael Fung, CS&E, The Chinese University of HK
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(); ? ? ? deck[2] (reference) deck[1] (reference) deck[0] (reference) Michael Fung, CS&E, The Chinese University of HK
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(); Octopus (class) Octopus (object) deck[2] (reference) deck[1] (reference) deck[0] (reference) Michael Fung, CS&E, The Chinese University of HK
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(); ? Octopus (class) Octopus (object) deck[2] (reference) deck[1] (reference) deck[0] (reference) Michael Fung, CS&E, The Chinese University of HK
Array Itself is Also a Reference deck[ ] (reference) Octopus (object) Octopus[] deck; deck = new Octopus[3]; deck[0] = new Octopus(); deck[1] = deck[0]; deck[2] = new Octopus(); Octopus (class) Octopus (object) deck[2] (reference) deck[1] (reference) deck[0] (reference) Class type array Michael Fung, CS&E, The Chinese University of HK
Array Itself is Also a Reference i[ ] (reference) int[] i; i = new int[3]; i[0] = 7; i[1] = i[0]; i[2] = 9; i[2] (int) 9 i[1] (int) 7 i[0] (int) 7 Primitive type array Class type array Michael Fung, CS&E, The Chinese University of HK
7-Minute Break Michael Fung, CS&E, The Chinese University of HK
Assignment of the Whole Array i[ ] (reference) They refer to the same array! 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 i[2] (int) 9 i[1] (int) 7 i[0] (int) 7 j[ ] (reference) Michael Fung, CS&E, The Chinese University of HK
Assignment of the Whole Array i[ ] (reference) OR, Create another one! int[] i = new int[3]; int[] j; j = new int[5]; i[2] (int) 9 i[1] (int) 7 j[ ] (reference) i[0] (int) 7 j[4] (int) 0 j[3] (int) 0 j[2] (int) 0 j[1] (int) 0 j[0] (int) 0 Michael Fung, CS&E, The Chinese University of HK
Assignment of the Whole Array i[ ] (reference) Remember to Keep It Well! int[] i = new int[3]; int[] j; j = new int[5]; j = i; i[2] (int) 9 i[1] (int) 7 j[ ] (reference) i[0] (int) 7 j[4] (int) 0 j[3] (int) 0 Killed j[2] (int) 0 j[1] (int) 0 j[0] (int) 0 Michael Fung, CS&E, The Chinese University of HK
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 1.0 newGPA 4.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! Michael Fung, CS&E, The Chinese University of HK
GPAs[1] 4.0 GPAs[0] 4.0 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] 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! Michael Fung, CS&E, The Chinese University of HK
Employee (object) salary (int) 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 michael (reference) Michael Fung, CS&E, The Chinese University of HK
Employee (object) Employee (object) Employee (object) salary 1000 salary 2000 salary 5000 New Concept: Object Type Array Argument Passing victims (reference) 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; } } Start here TAs[0] (reference) TAs (reference) TAs[1] (reference) TAs[2] (reference) 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[] 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 Michael Fung, CS&E, The Chinese University of HK
Duplicating an int Array i[ ] (reference) Copy the elements one-by-one int[] i = {7, 7, 9}; int[] j; j = new int[i.length]; for (int count = 0; count < i.length; count++) j[count] = i[count]; i[2] (int) 9 i[1] (int) 7 i[0] (int) 7 Michael Fung, CS&E, The Chinese University of HK
Duplicating an int Array i[ ] (reference) Copy the elements one-by-one int[] i = {7, 7, 9}; int[] j; j = new int[i.length]; for (int count = 0; count < i.length; count++) j[count] = i[count]; i[2] (int) 9 i[1] (int) 7 j[ ] (reference) i[0] (int) 7 ? Michael Fung, CS&E, The Chinese University of HK
Duplicating an int Array i[ ] (reference) Copy the elements one-by-one int[] i = {7, 7, 9}; int[] j; j = new int[i.length]; for (int count = 0; count < i.length; count++) j[count] = i[count]; i[2] (int) 9 i[1] (int) 7 j[ ] (reference) i[0] (int) 7 j[2] (int) 0 j[1] (int) 0 j[0] (int) 0 Michael Fung, CS&E, The Chinese University of HK
Duplicating an int Array i[ ] (reference) Copy the elements one-by-one int[] i = {7, 7, 9}; int[] j; j = new int[i.length]; for (int count = 0; count < i.length; count++) j[count] = i[count]; i[2] (int) 9 i[1] (int) 7 j[ ] (reference) i[0] (int) 7 j[2] (int) 9 j[1] (int) 7 j[0] (int) 7 Michael Fung, CS&E, The Chinese University of HK
Duplicating an Object Array deck[ ] (reference) Octopus (object) Octopus[] deck; ... Octopus[] newDeck; newDeck = new Octopus[deck.length]; for (int count = 0; count < deck.length; count++) newDeck[count] = deck[count]; Octopus (class) Octopus (object) deck[2] (reference) deck[1] (reference) deck[0] (reference) Michael Fung, CS&E, The Chinese University of HK
Duplicating an Object Array newDeck[ ] (reference) deck[ ] (reference) Octopus (object) Octopus[] deck; ... Octopus[] newDeck; newDeck = new Octopus[deck.length]; for (int count = 0; count < deck.length; count++) newDeck[count] = deck[count]; Octopus (class) Octopus (object) ? deck[2] (reference) deck[1] (reference) deck[0] (reference) Michael Fung, CS&E, The Chinese University of HK
Duplicating an Object Array newDeck[ ] (reference) deck[ ] (reference) Octopus (object) Octopus[] deck; ... Octopus[] newDeck; newDeck = new Octopus[deck.length]; for (int count = 0; count < deck.length; count++) newDeck[count] = deck[count]; Octopus (class) Octopus (object) newDeck[2] (reference) deck[2] (reference) newDeck[1] (reference) deck[1] (reference) newDeck[0] (reference) deck[0] (reference) Michael Fung, CS&E, The Chinese University of HK
Duplicating an Object Array newDeck[ ] (reference) deck[ ] (reference) Octopus (object) Octopus[] deck; ... Octopus[] newDeck; newDeck = new Octopus[deck.length]; for (int count = 0; count < deck.length; count++) newDeck[count] = deck[count]; Octopus (class) Octopus (object) Only the object references are copied! newDeck[2] (reference) deck[2] (reference) newDeck[1] (reference) deck[1] (reference) newDeck[0] (reference) deck[0] (reference) 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 Michael Fung, CS&E, The Chinese University of HK