Presentation is loading. Please wait.

Presentation is loading. Please wait.

CSS161: Fundamentals of Computing

Similar presentations


Presentation on theme: "CSS161: Fundamentals of Computing"— Presentation transcript:

1 CSS161: Fundamentals of Computing
Arrays and References This slide set was compiled from the Absolute Java textbook slides (Walter Savitch) and the professor’s own class materials. CSS161: Fundamentals of Computing

2 CSS161: Fundamentals of Computing
Arrays Are Objects A reference variable double[] a; An array creation a = new double[10]; = and == operators int[] x = {1, 2, 3}; int[] y = {1, 2, 3}; if ( x == y ) System.out.println( “x and y are the same” ); int[] z = y; if ( y == z ) { System.out.println( “y and z are the same” ); z[2] = 10; // this means y[2] = 10 } a nothing a 0.0 a[0] a[1] a[2] a[8] a[9] 1 [0] 2 [1] 3 [2] x 1 [0] 2 [1] 3 [2] y 10 z CSS161: Fundamentals of Computing

3 Revisiting Array Initialization
Default values int[] age = new int[3]; double[] score = new double[10]; doolean[] passOrNot = new boolean[5]; How about Date[] holidayList = new Date[7]; age[0] age[1] age[2] score[0] 0.0 score[1] 0.0 score[2] 0.0 score[3] 0.0 score[4] 0.0 score[5] 0.0 score[6] 0.0 score[7] 0.0 passOrNot[0] score[8] false 0.0 passOrNot[1] score[9] false 0.0 passOrNot[2] false passOrNot[3] false passOrNot[4] false null holidayList[0] null holidayList[1] null holidayList[2] null holidayList[3] null holidayList[4] null holidayList[5] null holidayList[6] CSS161: Fundamentals of Computing

4 Arrays with a Class Base Type
Instantiate an object of a given class and assign it to each array element. holidayList[0] = new Date(); . . . holidayList[6] = new Date(); OR for (int i = 0; i < holidayList.length; i++) holidayList[i] = new Date(); holidayList[0] Date object holidayList[1] Date object holidayList[2] Date object holidayList[3] Date object holidayList[4] Date object holidayList[5] Date object holidayList[6] Date object CSS161: Fundamentals of Computing

5 CSS161: Fundamentals of Computing
Array Parameters Passing an independent array element A copy of the array element’s value is passed to a method double[] a = { 1.0, 1.414, 1.732, 2.0, } square( a[0], a[1], a[2], a[3], a[5] ); Passing an entire array A copy of the array’s reference is passed to a method double[] a = { 1.0, 1.414, 1.732, 2, } square( a ); CSS161: Fundamentals of Computing

6 Passing Independent Array Elements
1.0 a[0] 1.414 a[1] 1.732 a[2] 2.236 a[4] 2.0 a[3] public static void main( String[] args ) { double[] a = { 1.0, 1.414, 1.732, 2.0, }; square( a[0], a[1], a[2], a[3], a[4] ); for ( int index = 0; index < a.length; index++ ) System.out.println( “a[“ + index + “] = “ + a[index] ); } public static void square( double a0, double a1, doube a2, double a3, double a4 ) { System.out.println( “Square of a0 = “ + ( a0 *= a0 ) ); System.out.println( “Square of a0 = “ + ( a1*= a1 ) ); System.out.println( “Square of a0 = “ + ( a2 *= a2 ) ); System.out.println( “Square of a0 = “ + ( a3 *= a3 ) ); System.out.println( “Square of a0 = “ + ( a4 *= a4 ) ); 1.0 1.999 2.999 4.0 4.999 1.0 1.414 1.732 2.0 2.236 CSS161: Fundamentals of Computing

7 Passing an Entire Array
1.0 a[0] 1.414 a[1] 1.732 a[2] 2.236 a[4] 2.0 a[3] length: 5 a 1.0 1.999 2.999 4.999 4.0 public static void main( String[] args ) { double[] a = { 1.0, 1.414, 1.732, 2.0, }; square( a ); for ( int index = 0; index < a.length; index++ ) System.out.println( “a[“ + index + “] = “ + a[index] ); } public static void square( double[] a ) { System.out.println( “Square of a[index] = “ + ( a[index] *= a[index] ) ); No need to specify the length [] is necessary to specify an array parameter CSS161: Fundamentals of Computing

8 CSS161: Fundamentals of Computing
Copying an Array Pitfall: use of = with arrays Copying just a reference, (I.e., shallow copy) int[] score = {1, 2, 3, 4, 5}; int[] copy = score; copy[3] = 10; System.out.println( “score[3] = “ + score[3] + “ copy[3] = “ + copy[3] ); Deep copy Copying each array element int[] copy = new int[score.length]; for ( int index = 0; index < length; length++ ) copy[index] = score[index]; [0] [1] [2] [3] [4] 1 2 3 4 5 10 10 [0] [1] [2] [3] [4] 1 2 3 4 5 [0] [1] [2] [3] [4] 1 2 3 10 4 5 4 10 CSS161: Fundamentals of Computing

9 CSS161: Fundamentals of Computing
Comparing Arrays Pitfall: use == with arrays Comparing just references int[] score1 = {1, 2, 3, 4, 5}; int[] score2 = score1; int[] score3 = {1, 2, 3, 4, 5}; if ( score1 == score2 ) System.out.println( “Their references are identical” ); if ( score1 != score3 ) System.out.println( “Their references are different” ); Comparing each array element int[] score2 = {1, 2, 3, 4, 5}; if ( score1.length != score2.length ) System.out.println( “different” ); else { int index; for ( index = 0; index < socre1.length; index++ ) if ( score1[index] != socre2[index] ) { break; } if ( index == score1.length ) System.out.println( “identical” ); [0] [1] [2] [3] [4] 1 2 3 4 5 [0] [1] [2] [3] [4] 1 2 3 4 5 [0] [1] [2] [3] [4] 1 2 3 4 5 [0] [1] [2] [3] [4] 1 2 3 4 5 CSS161: Fundamentals of Computing

10 CSS161: Fundamentals of Computing
Example public class ArrayComp { public static void main( String[] args ) { int[] score1 = { 1, 2, 3, 4, 5 }; int[] score2 = new int[score1.length]; for ( int index = 0; index < score1.length; index++ ) score2[index] = score1[index]; System.out.println( "Are socre1 and score2 identical? " + compareArray( score1, score2 ) ); int[] score3 = { 1, 2, 3, 4, 10 }; System.out.println( "Are socre1 and score3 identical? " + compareArray( score1, score3 ) ); } public static boolean compareArray( int[] array1, int[] array2 ) { if ( array1.length != array2.length ) { return false; }else { for ( int index = 0; index < array1.length; index++ ) if ( array1[index] != array2[index] ) return true; [0] [1] [2] [3] [4] 1 2 3 4 5 [0] [1] [2] [3] [4] 1 2 3 4 5 [0] [1] [2] [3] [4] 1 2 3 4 10 CSS161: Fundamentals of Computing

11 CSS161: Fundamentals of Computing
Self-Test Exercises Work on textbook p350 and p351’s exercises 7 ~ 10. CSS161: Fundamentals of Computing

12 Argument for the Method main
The main method of a program has a parameter for an array of String. public static void main(String[] args) When JVM starts a program with user arguments, they are passed to args[0] …args[args.length - 1] public class SomeProgram { public static void main(String[] args) { for ( int index = 0; index < args.length; index++ ) System.out.println(args[index]); } arg[0] “hello” arg[1] “world” arg[2] “123” % java SomeProgram hello world 123 hello world 123 % CSS161: Fundamentals of Computing

13 Methods that Return an Array
Syntax public static Base_Type[] Method_Name( Parameter_List ) { Base_Type[] temp = new Base_Type[Array_Size]; // Some code to fill temp goes here. return temp; } Example public static int[] incrementArray( int[] a, int increment ) int[] temp = new int[a.length]; for ( int index = 0; index < a.length; index++ ) temp[index] = a[index] + increment; CSS161: Fundamentals of Computing

14 Mechanism to Return a Value
1 2 3 4 5 [0] [1] [2] [3] [4] length: 5 public class SomeProgram { public static void main( String[] args ) { int[] original = {1, 2, 3, 4, 5} int[] incremented = incrementArray( original, 10 ); } public static int[] incrementArray( int[] a, int increment ) { int[] temp = new int[a.length]; int index; for ( index = 0; index < a.length; index++ ) temp[index] = a[index] + increment; return temp; Parameter a points to what original points to reference original copied Stored into incremented 0 ~ 5 [0] [1] [2] [3] [4] length: 5 11 12 13 14 15 A copy of reference temp is created. CSS161: Fundamentals of Computing

15 CSS161: Fundamentals of Computing
Self-Test Exercises Work on textbook p356’s exercises 11 ~ 12. CSS161: Fundamentals of Computing


Download ppt "CSS161: Fundamentals of Computing"

Similar presentations


Ads by Google