Survey
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project
Java Programming :Array Seree Chinodom 1 Objects for Organizing Data Arrays The Vector Class Strings Revisited 2 The CS Data Challenge Manipulate Data Add Delete Replace Modify Search Sort Etc... Organize Data Class Subclass Array Linked List Tree Binary Tree Etc... 3 Scores Write a Java program that analyzes 10 test scores. The program should... – …compute the sum of all of the test scores, – ...compute the average score, and – ...determine whether the test was too easy. 4 // Scores01 - average 10 exam scores with ten variables public class Scores { public static void main (String[] args) { int count = 10; int score_0 = 0; int score_1 = 0; int score_2 = 0; int score_3 = 0; int score_4 = 0; int score_5 = 0; int score_6 = 0; int score_7 = 0; int score_8 = 0; int score_9 = 0; int sum = 0; int average = 0; 5 score_0 = 98; score_1 = 82; score_2 = 94; score_3 = 87; score_4 = 63; score_5 = 99; score_6 = 85; score_7 = 93; score_8 = 90; score_9 = 75; sum = score_0 + score_1 + score_2 +score_3 + score_4 + score_5 + score_6 +score_7 +score_8 +score_9; 6 average = Math.round((float) sum / count); System.out.println("sum " + sum); System.out.println("average " + average); if (average > 60) System.out.println("Too easy!"); } // main } // Scores sum 866 average 87 Too easy! 7 Observations Above Task Very Common – – – – – – – driver_0 driver_1 driver_2 driver_3 car_0 car_1 car_2 Key use of computers... Operate quickly on many, many things Above Technique... Not general Does not scale Need New… – Data organizing mechanism – Data structure 8 Array Ordered List of “Elements” – Primitive data – Objects Elements… – Numbered (“indexed”) starting with 0 – Referenced via index Length = Number of Elements AKA – List – Vector 9 Array Illustrated Score Element 98 82 Index [0] [1] 94 87 Length=10 63 99 85 93 90 75 [9] 10 Java Arrays We must learn how to… declare, instantiate, and reference …Java arrays. 11 Declaring Arrays type[] variable; – Declares variable… – – – – • …to be a pointer to an array • … whose elements are of type type. Reserves memory for one pointer Does not specify length of array Does not reserve memory for array itself Default value of variable is null Arrays are objects! Examples int[] score; // allocates pointer to array of ints float[] gpa; // allocates pointer to array of floats char[] name; // allocates pointer to array of chars 12 Declaring Arrays Illustrated Address Value 1024 1028 Null Int[] score; Identifier score 1032 1036 1040 1044 1048 1052 1056 1060 1064 1068 1072 1076 1080 1084 1088 1092 13 Instantiating Arrays new type[ expression] Allocates an array of… – Type type – Length expression – .length == expression Examples – score = new int[10]; // allocates array of 10 ints // score.length == 10 – gpa = new float[420]; // allocates array of 420 floats – name = new char[128] // allocates array of 128 chars [] vs. () – [] Used for arrays – () Used for methods including constructors 14 Instantiating Arrays Illustrated Address Value 1024 score = new int[10]; 1028 1048 Identifier score 1032 1036 1040 1044 1048 0 1052 0 1056 0 1060 0 1064 0 1068 0 1072 0 1076 0 1080 0 1084 0 1088 1092 [0] [1] [9] 15 Referencing Arrays variable[ expression] – [] is an operator – References expression th element – expression ranges from 0 to length - 1 Examples – score[0] = 98; score[9] = 75; – gpa[390] = 4.0; – name[0] = 'D'; name[1] = 'y'; name[2] = 'k'; name[3] = 's'; name[4] = 'e'; name[5] = 'n'; Read... “score of 0” or “score sub 0” “gpa of 390” or “gpa sub 390” “name of 4” or “name sub 4” 16 Referencing Arrays Illustrated Address Value 1024 score[0] = 98; score[9] = 75; 1028 1048 Identifier score 1032 1036 1040 1044 1048 98 1052 0 1056 0 1060 0 1064 0 1068 0 1072 0 1076 0 1080 0 1084 75 1088 1092 [0] [1] [9] 17 Array Referencing Caveat Once Instantiated, Array Sizes Are Fixed Index Must Be… – >= 0 – <= .length - 1 Bounds Checking – Automatic by JVM – Checks array index, 0 < = index <= .length - 1 – Throws exception: “Array Reference Out of Bounds” 18 Off-By-One Problems int[] score = new int[10]; for (int i = 1; i < score.length; i++) // Error? score[i] = 100; for (int i = 0; i <= score.length; i++) // Error? score[i] = 100; for (int i = 0; i++ < score.length;) // Error? score[i] = 100; for (int i = 0; ++i < score.length;) // Error? score[i] = 100; for (int i = 0; i < score.length;) // Error? score[++i] = 100; for (int i = 0; i < score.length;) // Error? score[i++] = 100; 19 Scores Write a Java program that analyzes 10 test scores. The program should... …compute the sum of all of the test scores, ...compute the average score, and ...determine whether the test was too easy. 20 // Scores02 - average 10 exam scores with one array public class Scores { public static void main (String[] args) { int count = 10; int[] score = null; int sum = 0; int average = 0; score = new int[count]; score[0] = 98; score[9] = 75; score[1] = 82; score[2] = 94; score[3] = 87; score[4] = 63; score[5] = 99; score[6] = 85; score[7] = 93; score[8] = 90; 21 sum = 0; for (int i = 0; i < score.length; i++) sum += score[i]; average = Math.round((float) sum / score.length); System.out.println("sum " + sum); System.out.println("average " + average); if (average > 60) System.out.println("Too easy!"); } // main } // Scores sum 866 average 87 Too easy! 22 Declare score Address int count = 10; int[] score = null; int sum = 0; int average = 0; Value 1024 10 1028 Null 1032 0 1036 0 Identifier count score sum average 1040 1044 1048 1052 1056 1060 1064 1068 1072 1076 1080 1084 1088 1092 23 Instantiating score Address score = new int[count]; Value 1024 10 1028 1048 1032 0 1036 0 Identifier count score sum average 1040 1044 1048 0 1052 0 1056 0 1060 0 1064 0 1068 0 1072 0 1076 0 1080 0 1084 0 1088 1092 [0] [1] [9] 24 Assigning Values to score Address score[0] = 98; score[9] = 75; Value 1024 10 1028 1048 1032 0 1036 0 Identifier count score sum average 1040 1044 1048 98 1052 0 1056 0 1060 0 1064 0 1068 0 1072 0 1076 0 1080 0 1084 75 1088 1092 [0] [1] [9] 25 Assigning More Values to score Address score[1] = 82; score[2] = 94; score[3] = 87; score[4] = 63; score[5] = 99; score[6] = 85; score[7] = 93; score[8] = 90; Value 1024 10 1028 1048 1032 0 1036 0 Identifier count score sum average 1040 1044 1048 98 1052 82 1056 94 1060 87 1064 63 1068 99 1072 85 1076 93 1080 90 1084 75 1088 1092 [0] [1] [9] 26 Average the score sum = 0; for (int i = 0; i < score.length; i++) sum += score[i]; average = Math.round((float) sum / score.length); System.out.println("sum " + sum); System.out.println("average " + average); if (average > 60) System.out.println("Too easy!"); sum 866 average 87 Too easy! Address Value 1024 10 1028 1048 1032 866 1036 87 Identifier count score sum average 1040 1044 1048 98 1052 82 1056 94 1060 87 1064 63 1068 99 1072 85 1076 93 1080 90 1084 75 [0] [1] [9] 1088 1092 27 Array Initializer List Used to Instantiate and Initialize an Array { value_1, value_2,…, value_n} – new not used – Length not specified (implicit) – Allowed only with declaration Examples – int[] score = {98, 82, 94, 87, 63, 99, 85, 93, 90, 75}; – char[] name = {'D', 'y', 'k', 's', 'e', 'n'}; – char[] digits = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}; 28 // Scores03 - average 10 exam scores with an initializer list public class Scores { public static void main (String[] args) { int[] score = {98, 82, 94, 87, 63, 99, 85, 93, 90, 75}; int sum = 0; int average = 0; sum = 0; for (int i = 0; i < score.length; i++) sum += score[i]; average = Math.round((float) sum / score.length); System.out.println("length " + score.length); System.out.println("sum " + sum); System.out.println("average " + average); if (average > 60) System.out.println("Too easy!"); length 10 } // main sum 866 } // Scores average 87 Too easy! 29 Array of Objects Array Declaration – Allocates a single pointer to an array of pointers – Rectangle[] rectangle; Array Instantiation – Allocates an array of pointers to objects – rectangle = new Rectangle[4]; Array Element Instantiation – Allocates one object pointed to by one array element – rectangle[0] = new Rectangle(); 30 Echo.java // Echo - echoes the command line arguments public class Echo { public static void main (String[] args) { for (int i = 0; i < args.length; i++) System.out.println("args[" + i + "] = " + args[i]); } // main C:\>java Echo CS 180 Rocks ! } // Echo args[0] = CS args[1] = 180 args[2] = Rocks args[3] = ! 31 Rectangles Class Data Methods – width – height Constructors height width – Rectangle(int, int) – Rectangle(int) – Rectangle() – – – – – – – void setWidth(int) int getWidth() void setHeight(int) int getHeight() int area() String toString() boolean equals() 32 // Rectangles14 - array of rectangles public class Main { public static void main(String[] args) { int total_area = 0; Rectangle[] rectangle = null; rectangle = new Rectangle[4]; rectangle[0] = new Rectangle(); System.out.println( rectangle[0] +"\n" + rectangle[0].getWidth() + "\n" + rectangle[0].getHeight()+ "\n" + rectangle[0].area() ); 3 X4 3 4 12 5 X7 5 7 35 3 X4 5 X7 8 X 10 12 X 12 271 33 rectangle[1] = new Rectangle(5, 7); System.out.println( rectangle[1] + "\n" + rectangle[1].getWidth() + "\n" + rectangle[1].getHeight() + "\n" + rectangle[1].area() ); rectangle[2] = new Rectangle(8, 10); rectangle[3] = new Rectangle(12); for (int i = 0; i < rectangle.length; i++) System.out.println(rectangle[i]); total_area = 0; for (int i = 0; i < rectangle.length; i++) total_area += rectangle[i].area(); System.out.println(total_area); } // main } // Main 3 X4 3 4 12 5 X7 5 7 35 3 X4 5 X7 8 X 10 12 X 12 271 34 Declaring rectangle Address int total_area = 0; Rectangle[] rectangle = null; Value Identifier 1024 0 1028 null total_area rectangle 1032 1036 1040 1044 1048 1052 1056 1060 1064 1068 1072 1076 1080 1084 1088 1092 35 Instantiating rectangle Address rectangle = new Rectangle[4]; Value Identifier 1024 0 1028 1036 1032 1036 null 1040 null 1044 null 1048 null total_area rectangle [0] [1] [2] [3] 1052 1056 1060 1064 1068 1072 1076 1080 1084 1088 1092 36 Instantiating rectangle[0] Address Rectangle[0] = new Rectangle(); System.out.println( rectangle[0] + "\n" + rectangle[0].getWidth() + "\n" + rectangle[0].getHeight() + "\n" + rectangle[0].area() 3 X4 3 ); 4 12 Value Identifier 1024 0 1028 1036 1032 1036 1060 1040 null 1044 null 1048 null total_area rectangle [0] [1] [2] [3] 1052 1056 1060 3 1064 4 width height 1068 1072 1076 1080 1084 1088 1092 37 Instantiating rectangle[1] Address Rectangle[1] = new Rectangle(5,7); System.out.println( rectangle[1] + "\n" + rectangle[1].getWidth() + "\n" + rectangle[1].getHeight() + "\n" + rectangle[1].area() 5 X7 5 ); 7 35 Value Identifier 1024 0 1028 null 1032 1036 1060 1040 1072 1044 null 1048 null total_area rectangle [0] [1] [2] [3] 1052 1056 1060 3 1064 4 1068 1072 5 1076 7 width height width height 1080 1084 1088 1092 38 Instantiating rectangle[2], rectangle[3] Address Rectangle[2] = new Rectangle(8, 10); Rectangle[3] = new Rectangle(12); Value Identifier 1024 0 1028 1036 1032 1036 1060 1040 1072 1044 1080 1048 1088 total_area rectangle [0] [1] [2] [3] 1052 1056 1060 3 1064 4 1068 1072 5 1076 7 1080 8 1084 10 1088 12 1092 12 width height width height width height width height 39 Using rectangle Address for (int i = 0; i < rectangle.length; i++) System.out.println(rectangle[i]); total_area = 0; for (int i = 0; i < rectangle.length; i++) total_area += rectangle[i].area(); System.out.println(total_area); Value Identifier 1024 271 1028 1036 1032 1036 1060 1040 1072 1044 1080 1048 1088 [0] [1] [2] [3] 1052 1056 1060 3 1064 4 1068 3 x4 5 x7 8 x 10 12 x 12 271 total_area rectangle 1072 5 1076 7 1080 8 1084 10 1088 12 1092 12 width height width height width height width height 40 “Multidimensional” Arrays An Array of… ... arrays of... There really are no …arrays of arrays of… “multidimensional” …arrays of arrays of arrays of… arrays in Java. …arrays of arrays of arrays of…etc…of arrays of... ...Primitive Data or Objects 41 Declaring Multidimensional Arrays type[][]…[] variable; Use n []’s for n dimensional array Examples – int[] array_1d; // array of ints – int[][] array_2d; // array of arrays of ints – int[][][] array_3d; // array of arrays of arrays of ints 42 Instantiating Multidimensional Arrays new type[ value_1][ value_2]…[ value_n] Use n []’s for n dimensional array Examples – array_1d = new int[3]; – array_2d = new int[3][4]; – array_3d = new int[3][4][5]; 43 Referencing Arrays variable type[ value_1][ value_2]…[ value_n] Use n []’s for n dimensional array Examples – array_1d[1] = 7; – array_2d[2][3] = 14; – array_3d[1][0][2] = 55; 44 Times Table int[][] times_table = new int[10][10]; for (i = 1; i < 10; i++) for (j = 1; j < 10; j++) times_table[i][j] = i * j; for (i = 1; i < 10; i++) { System.out.println(); for (j = 1; j < 10; j++) System.out.print(df.format(times_table[i][j]) + " "); } 01 02 03 04 05 06 07 08 09 02 04 06 08 10 12 14 16 18 03 06 09 12 15 18 21 24 27 04 08 12 16 20 24 28 32 36 05 10 15 20 25 30 35 40 45 45 // TimesTable - prints a times table using doubly-nested for loops import java.text.DecimalFormat; public class Main { public static void main (String[] args) { final int X_LIMIT = 10; int[][] times_table = new int[X_LIMIT][X_LIMIT]; DecimalFormat df = new DecimalFormat("00"); int i,j; for (i = 1; i < X_LIMIT; i++) for (j = 1; j < X_LIMIT; j++) times_table[i][j] = i * j; for (i = 1; i < X_LIMIT; i++) { System.out.println(); for (j = 1; j < X_LIMIT; j++) System.out.print(df.format(times_table[i][j]) + " "); } } // main } // Main 46 array_2d - Who’s on first? int[][] array_2d; array_2d A Pointer to an Array of Pointers to an Array of int’s array_2d[0] A Pointer to an Array of Int’s (array_2d)[0] array_2d[0][0] An Int (array_2d[0])[0] ((array_2d)[0])[0] 47 Using array_2d int[][] array_2d; // pointer array_2d = new int[4][]; // array of 4 pointers array_2d.length == 4 array_2d[0] = new int[4]; // array of 4 ints array_2d[0].length == 4 array_2d[1] = new int[2]; // array of 2 ints array_2d[1].length == 2 array_2d[2] = new int[3]; // array of 3 ints array_2d[2].length == 3 array_2d[3] = new int[2]; // array of 2 ints array_2d[3].length == 2 48 // Multidimensional - multidimensional arrays import java.text.DecimalFormat; public class Main { public static void main (String[] args) { int[][] array_2d; array_2d = new int[4][]; array_2d[0] = new int[4]; array_2d[0][0] = 00; array_2d[0][1] = 01; array_2d[0][2] = 02; array_2d[0][3] = 03; array_2d[1] = new int[2]; array_2d[1][0] = 10; array_2d[1][1] = 11; 49 array_2d[2] = new int[3]; array_2d[2][0] = 20; array_2d[2][1] = 21; array_2d[2][2] = 22; array_2d[3] = new int[2]; array_2d[3][0] = 30; array_2d[3][1] = 31; printArray2D("array_2d", array_2d); int[] array_1d; array_1d = array_2d[2]; printArray1D("array_1d", array_1d); array_1d[0] = 10020; array_1d[1] = 10021; array_1d[2] = 10022; printArray1D("array_1d", array_1d); printArray1D("array_2d", array_2d[2]); 50 array_2d[2][0] = 20; array_2d[2][1] = 21; array_2d[2][2] = 22; printArray1D("array_1d", array_1d); printArray1D("array_2d", array_2d[2]); } // main public static void printArray2D(String identifier, int[][] array) { DecimalFormat df = new DecimalFormat("00"); for (int i = 0; i < array.length; i++) { System.out.print("\n" + identifier); for (int j = 0; j < array[i].length; j++) System.out.print( "[" + i + "]" + "[" + j + "]" + "(" + df.format(array[i][j]) + ") " ); } System.out.println(); } // printArray2D 51 public static void printArray1D(String identifier, int[] array) { DecimalFormat df = new DecimalFormat("00"); System.out.print("\n" + identifier); for (int i = 0; i < array.length; i++) System.out.print( "[" + i + "]" + "(" + df.format(array[i]) + ") " ); System.out.println(); } // printArray1D array_2d[0][0] (00) [0][1](01) [0][2](02) [0][3](03) } // main array_2d[1][0] (10) [1][1](11) array_2d[2][0] (20) [2][1](21) [2][2](22) array_2d[3][0] (30) [3][1](31) array_1d[0] (20) [1](21) [2](22) array_1d[0] (10020) [1](10021) [2](10022) array_2d[0] (10020) [1](10021) [2](10022) array_1d[0] (20) [1](21) [2](22) array_2d[0] (20) [1](21) [2](22) 52 Declaring array_2d (A Pointer to an Array of Pointers to an Array of int’s) Address int[][] array_2d; Value 1024 null 1028 Identifier array_2d 1032 1036 1040 1044 1048 1052 1056 1060 1064 1068 1072 1076 1080 1084 1088 1092 53 Instantiating array_2d (A Pointer to an Array of Pointers to an Array of int’s) Address array_2d = new int[4][]; Value 1024 1028 1028 null 1032 null 1036 null 1040 null 1044 Identifier array_2d [0] [1] [2] [3] 1048 1052 1056 1060 1064 1068 1072 1076 1080 1084 1088 1092 54 Instantiating array_2d[0] (A Pointer to Array of int’s) Address array_2d[0] = new int[4][]; Value Identifier 1024 1028 1028 1044 1032 null 1036 null 1040 null 1044 0 1048 0 1052 0 1056 0 1060 array_2d [0] [1] [2] [3] [0] [1] [2] [3] 1064 1068 1072 1076 1080 1084 1088 1092 55 Using array_2d[0] (A Pointer to Array of int’s) Address array_2d[0][0] = 00; array_2d[0][1] = 01; array_2d[0][2] = 02; array_2d[0][3] = 03; Value 1024 1028 1028 1044 1032 null 1036 null 1040 null 1044 00 1048 01 1052 02 1056 03 1060 Identifier array_2d [0] [1] [2] [3] [0] [1] [2] [3] 1064 1068 1072 1076 1080 1084 1088 1092 56 Intantiating array_2d[1] (A Pointer to Array of int’s) Address array_2d[1] = new int[2]; Value Identifier 1024 1028 1028 1044 1032 1060 1036 null 1040 null 1044 00 1048 01 1052 02 1056 03 1060 0 1064 0 array_2d [0] [1] [2] [3] [0] [1] [2] [3] [0] [1] 1068 1072 1076 1080 1084 1088 1092 57 Using array_2d[1] (A Pointer to Array of int’s) Address array_2d[1][0] = 10; array_2d[1][1] = 11; Value 1024 1028 1028 1044 1032 1060 1036 null 1040 null 1044 00 1048 01 1052 02 1056 03 1060 10 1064 11 Identifier array_2d [0] [1] [2] [3] [0] [1] [2] [3] [0] [1] 1068 1072 1076 1080 1084 1088 1092 58 array_2d[2] and array_2d[3] (A Pointer to Array of int’s) Address array_2d[2] = new int[3]; array_2d[2][0] = 20; array_2d[2][1] = 21; array_2d[2][2] = 22; array_2d[3] = new int[2]; array_2d[3][0] = 30; array_2d[3][1] = 31; Value 1024 1028 1028 1044 1032 1060 1036 1068 1040 1080 1044 00 1048 01 1052 02 1056 03 1060 10 1064 11 1068 20 1072 21 1076 22 1080 30 1084 31 Identifier array_2d [0] [1] [2] [3] [0] [1] [2] [3] [0] [1] [0] [1] [2] [0] [1] 1088 1092 59 Declare array_1d (A Pointer to Array of int’s) Address int[] array_1d; Value 1024 1028 1028 1044 1032 1060 1036 1068 1040 1080 1044 00 1048 01 1052 02 1056 03 1060 10 1064 11 1068 20 1072 21 1076 22 1080 30 1084 31 1088 null 1092 Identifier array_2d [0] [1] [2] [3] [0] [1] [2] [3] [0] [1] [0] [1] [2] [0] [1] array_1d 60 Assigning a Value to array_1d (A Pointer to Array of int’s) Address // Error! Why? // array_1d = array_2d; array_1d = array_2d[2]; Value 1024 1028 1028 1044 1032 1060 1036 1068 1040 1080 1044 00 1048 01 1052 02 1056 03 1060 10 1064 11 1068 20 1072 21 1076 22 1080 30 1084 31 1088 null 1092 Identifier array_2d [0] [1] [2] [3] [0] [1] [2] [3] [0] [1] [0] [1] [2] [0] [1] array_1d 61 Using array_1d (A Pointer to Array of int’s) Address array_1d[0] = 10020; array_1d[1] = 10021; array_1d[2] = 10031; Value 1024 1028 1028 1044 1032 1060 1036 1068 1040 1080 1044 00 1048 01 1052 02 1056 03 1060 10 1064 11 1068 10020 1072 10021 1076 10031 1080 30 1084 31 1088 1068 1092 Identifier array_2d [0] [1] [2] [3] [0] [1] [2] [3] [0] [1] [0] [1] [2] [0] [1] array_1d 62 Using array_2d (A Pointer to Array of int’s) Address array_2d[2][0] = 20; array_2d[2][1] = 21; array_2d[2][2] = 22; Value 1024 1028 1028 1044 1032 1060 1036 1068 1040 1080 1044 00 1048 01 1052 02 1056 03 1060 10 1064 11 1068 20 1072 21 1076 22 1080 30 1084 31 1088 1068 1092 Identifier array_2d [0] [1] [2] [3] [0] [1] [2] [3] [0] [1] [0] [1] [2] [0] [1] array_1d 63 Initializing Multidimensional Arrays Use Lists of Initializer Lists Example int[][] array_2d = { {00, 01, 02, 03}, {10, 11}, {20, 21, 22}, {30, 31} }; Length – – – – – array_2d.length == 4 array_2d[0].length == 4 array_2d[1].length == 2 array_2d[2].length == 3 array_2d[3].length == 2 64 // Initializer - array initializer list import java.text.DecimalFormat; public class Main { public static void main (String[] args) { int[][] array_2d = { {00, 01, 02, 03}, {10, 11}, {20, 21, 22}, {30, 31} }; for (int i = 0; i < array_2d.length; i++) System.out.println( "array_2d[" + i + "].length = " + array_2d[i].length ); printArray2D("array_2d", array_2d); } // main 65 public static void printArray2D(String identifier, int[][] array) { DecimalFormat df = new DecimalFormat("00"); for (int i = 0; i < array.length; i++) { System.out.print("\n" + identifier); for (int j = 0; j < array[i].length; j++) System.out.print( "[" + i + "]" + "[" + j + "]" + "(" + df.format(array[i][j]) + ") " ); } System.out.println(); array_2d[0].length = 4 array_2d[1].length = 2 } // printArray2D array_2d[2].length = 3 } // Main array_2d[3].length = 2 array_2d[0][0](00) [0][1](01) [0][2](02) [0][3](03) array_2d[1][0](10) [1][1](11) array_2d[2][0](20) [2][1](21) [2][2](22) array_2d[3][0](30) [3][1](31) 68 66 Primitive vs. Object Parameters All parameters are passed by value. Value of actual parameter copied to local parameter Primitive – Value Copied? Value of primitive type – Can change value of local copy (of primitive type) – Cannot access actual parameter Object – – – – Value Copied? Reference (address) of object Can change value of local copy (of object address) Cannot access actual parameter Can access object pointed to by actual parameter 67 Arrays as Parameters Objects may be passed as parameters. Arrays… – ...are objects. – …may be passed as parameters. “Value” of an Array Variable – Address of array – Passed to methods 68 // Parameters - arrays as parameters import java.text.DecimalFormat; public class Main { static DecimalFormat df = new DecimalFormat("0000"); final static int I = 1; final static int J = 2; public static void main (String[] args) { int[][] array_2d = { {00, 01, 02}, {10, 11, 12}, }; 69 System.out.println("println 1: " + df.format(array_2d[I][J])); paramArray2D(array_2d); System.out.println("println 5: " + df.format(array_2d[I][J])); paramArray1D(array_2d[I]); System.out.println("println 9: " + df.format(array_2d[I][J])); paramInt(array_2d[I][J]); System.out.println("println 12: " + df.format(array_2d[I][J])); } // main println 1: 0012 println 2: 0012 println 3: 0112 println 4: 9999 println 5: 0112 println 6: 0112 println 7: 1112 println 8: 9999 println 9: 1112 println 10: 1112 println 11: 9999 println 12: 1112 72 70 public static void paramArray2D(int param_2d[][]) { System.out.println("println 2: " + df.format(param_2d[I][J])); param_2d[I][J] += 100; System.out.println("println 3: " + df.format(param_2d[I][J])); param_2d = new int[I + 1][J + 1]; param_2d[I][J] = 9999; System.out.println("println 4: " + df.format(param_2d[I][J])); } // paramArray2D println 1: 0012 println 2: 0012 println 3: 0112 println 4: 9999 println 5: 0112 println 6: 0112 println 7: 1112 println 8: 9999 println 9: 1112 println 10: 1112 println 11: 9999 println 12: 1112 71 public static void paramInt(int param_int) { System.out.println("println 10: " + df.format(param_int)); param_int = 9999; System.out.println("println 11: " + df.format(param_int)); } // paramInt } // Main println 1: 0012 println 2: 0012 println 3: 0112 println 4: 9999 println 5: 0112 println 6: 0112 println 7: 1112 println 8: 9999 println 9: 1112 println 10: 1112 println 11: 9999 println 12: 1112 72 public static void paramArray1D(int param_1d[]) { System.out.println("println 6: " + df.format(param_1d[J])); param_1d[J] += 1000; System.out.println("println 7: " + df.format(param_1d[J])); param_1d = new int[J + 1]; param_1d[J] = 9999; System.out.println("println 8: " + df.format(param_1d[J])); } // paramArray1D println 1: 0012 println 2: 0012 println 3: 0112 println 4: 9999 println 5: 0112 println 6: 0112 println 7: 1112 println 8: 9999 println 9: 1112 println 10: 1112 println 11: 9999 println 12: 1112 73 Array Parameter (Parameters.java) Address int[][] array_2d = { {00, 01, 02}, {10, 11, 12}, }; System.out.println( "println 1: " + array_2d[1][2] ); Value 1024 1028 1028 1036 1032 1048 1036 00 1040 01 1044 02 1048 10 1052 11 1056 12 1060 Identifier array_2d [0] [1] [0] [1] [2] [0] [1] [2] 1064 1068 1072 1076 1080 println 1: 0012 1084 1088 1092 74 Invoking paramArray2D() (array_2d -> param_2d) Address paramArray2D(array_2d); Value 1024 1028 1028 1036 1032 1048 1036 00 1040 01 1044 02 1048 10 1052 11 1056 12 1060 1028 Identifier array_2d [0] [1] [0] [1] [2] [0] [1] [2] param_2d 1064 1068 1072 1076 1080 1084 1088 1092 75 Executing paramArray2D() Address System.out.println( "println 2: " + param_2d[1][2] ); Value 1024 1028 1028 1036 1032 1048 1036 00 1040 01 1044 02 1048 10 1052 11 1056 12 1060 1028 Identifier array_2d [0] [1] [0] [1] [2] [0] [1] [2] param_2d 1064 1068 1072 1076 println 2: 0012 1080 1084 1088 1092 76 Executing paramArray2D() Address param_2d[1][2] += 100; System.out.println( "println 3: " + param_2d[1][2] ); Value 1024 1028 1028 1036 1032 1048 1036 00 1040 01 1044 02 1048 10 1052 11 1056 112 1060 1028 Identifier array_2d [0] [1] [0] [1] [2] [0] [1] [2] param_2d 1064 1068 1072 1076 println 3: 0112 1080 1084 1088 1092 77 Executing paramArray2D() Address param_2d = new int[2][3]; Value Identifier 1024 1028 1028 1036 1032 1048 1036 00 1040 01 1044 02 1048 10 1052 11 1056 112 1060 1064 1064 1072 1068 1084 1072 0 1076 0 1080 0 1084 0 1088 0 1092 0 array_2d [0] [1] [0] [1] [2] [0] [1] [2] param_2d [0] [1] [0] [1] [2] [0] [1] 78 Executing paramArray2D() Address param_2d[1][2] = 9999; System.out.println( "println 4: " + param_2d[1][2] ); println 4: 9999 Value Identifier 1024 1028 1028 1036 1032 1048 1036 00 1040 01 1044 02 1048 10 1052 11 1056 112 1060 1064 1064 1072 1068 1084 1072 0 1076 0 1080 0 1084 0 1088 0 1092 9999 array_2d [0] [1] [0] [1] [2] [0] [1] [2] param_2d [0] [1] [0] [1] [2] [0] [1] 79 Returning to main() Address System.out.println( "println 5: " + array_2d[1][2] ); Value 1024 1028 1028 1036 1032 1048 1036 00 1040 01 1044 02 1048 10 1052 11 1056 112 1060 Identifier array_2d [0] [1] [0] [1] [2] [0] [1] [2] 1064 1068 1072 1076 println 5: 0112 1080 1084 1088 1092 80 Invoking paramArray1D() (array_2d[1] -> param_1d) Address paramArray1D(array_2d[1]); Value 1024 1028 1028 1036 1032 1048 1036 00 1040 01 1044 02 1048 10 1052 11 1056 112 1060 1048 Identifier array_2d [0] [1] [0] [1] [2] [0] [1] [2] param_1d 1064 1068 1072 1076 1080 1084 1088 1092 81 Executing paramArray1D() Address System.out.println( "println 6: " + param_1d[2] ); Value 1024 1028 1028 1036 1032 1048 1036 00 1040 01 1044 02 1048 10 1052 11 1056 112 1060 1048 Identifier array_2d [0] [1] [0] [1] [2] [0] [1] [2] param_1d 1064 1068 1072 1076 println 6: 0112 1080 1084 1088 1092 82 Executing paramArray1D() Address param_1d[2] += 1000; System.out.println( "println 7: " + param_1d[2] ); Value 1024 1028 1028 1036 1032 1048 1036 00 1040 01 1044 02 1048 10 1052 11 1056 1112 1060 1048 Identifier array_2d [0] [1] [0] [1] [2] [0] [1] [2] param_1d 1064 1068 1072 1076 println 7: 1112 1080 1084 1088 1092 83 Executing paramArray1D() Address param_1d = new int[3]; Value Identifier 1024 1028 1028 1036 1032 1048 1036 00 1040 01 1044 02 1048 10 1052 11 1056 1112 1060 1064 1064 0 1068 0 1072 0 1076 array_2d [0] [1] [0] [1] [2] [0] [1] [2] param_1d [0] [1] [2] 1080 1084 1088 1092 84 Executing paramArray1D() Address param_1d[2] = 9999; System.out.println( "println 8: " + param_1d[2] ); Identifier 1024 1028 1028 1036 1032 1048 1036 00 1040 01 1044 02 1048 10 1052 11 1056 1112 1060 1064 1064 0 1068 0 1072 9999 1076 println 8: 9999 Value array_2d [0] [1] [0] [1] [2] [0] [1] [2] param_1d [0] [1] [2] 1080 1084 1088 1092 85 Returning to main() Address System.out.println( "println 9: " + array_2d[1][2] ); Value 1024 1028 1028 1036 1032 1048 1036 00 1040 01 1044 02 1048 10 1052 11 1056 1112 1060 Identifier array_2d [0] [1] [0] [1] [2] [0] [1] [2] 1064 1068 1072 1076 println 9: 1112 1080 1084 1088 1092 86 Invoking paramInt() (array_2d[1][2] -> param_int) Address paramInt(array_2d[1][2]); Value 1024 1028 1028 1036 1032 1048 1036 00 1040 01 1044 02 1048 10 1052 11 1056 1112 1060 1112 1064 Identifier array_2d [0] [1] [0] [1] [2] [0] [1] [2] para_int 1068 1072 1076 1080 1084 1088 1092 87 Executing paramInt() Address System.out.println( "println 10: " + param_int ); Value 1024 1028 1028 1036 1032 1048 1036 00 1040 01 1044 02 1048 10 1052 11 1056 1112 1060 1112 1064 Identifier array_2d [0] [1] [0] [1] [2] [0] [1] [2] para_int 1068 1072 1076 println 10: 1112 1080 1084 1088 1092 88 Executing paramInt() Address param_int = 9999; System.out.println( "println 11: " + param_int ); Value 1024 1028 1028 1036 1032 1048 1036 00 1040 01 1044 02 1048 10 1052 11 1056 1112 1060 9999 1064 Identifier array_2d [0] [1] [0] [1] [2] [0] [1] [2] para_int 1068 1072 1076 println 11: 9999 1080 1084 1088 1092 89 Executing paramInt() Address System.out.println( "println 12: " + array_2d[1][2] ); Value 1024 1028 1028 1036 1032 1048 1036 00 1040 01 1044 02 1048 10 1052 11 1056 1112 1060 Identifier array_2d [0] [1] [0] [1] [2] [0] [1] [2] 1064 1068 1072 1076 println 12: 1112 1080 1084 1088 1092 90