









Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!










Department of Computer Science Wellesley College
Friday, November 9, 2007 Arrays 17- 2
0 1 2 String [] words “I” “am” “Sam” ExecutionLand (^) ObjectLand
Arrays 17- 3 Anatomy of an Array Diagram 0 1 2 String [] “I” “am” “Sam” ObjectLand an array object slot indices elements slots (variables) the type of the array object (T [ ] is pronounced “array of T” ) Arrays 17- 4 Arrays Are Homogeneous Collections 0 false boolean [] tests 1 false 2 false 3 true 0 4 1 7 3 8 2 3 4 6 int [] nums 0 1 Location [] line x y Location x y Location 5 2
0 1 2 String [] words “I” “am” “Sam”
Arrays 17- 7 Array Indexing
0 false boolean [] tests 1 false 2 false 3 true 0 4 1 7 3 8 2 3 4 6 int [] nums 0 1 Location [] line x y Location x y Location 5 2
0 1 2 String [] words “I” “am” “Sam”
Arrays 17- 9 Array Slots are Mutable
0 4 1 7 3 8 2 3 4 6 int [] nums 0 17 1 7 3 8 2 3 4 6 int [] nums 0 17 1 11 3 8 2 3 4 6 int [] nums 0 17 1 11 3 8 2 3 4 7 int [] nums nums[0] = 17; nums[1] = nums[2] + nums[3]; nums[4]++; // means nums[4] = nums[4] + 1; Arrays 17- 10 Swapping Array Elements 0 1 2 String []
“I” “am” “Sam” 0 1 2 String []
“Sam”“am” “I” 0 1 2 String []
“Sam” “I” “am” swap(words,0,2); swap(words,1,2); public static void swap (String [] strings, int i, int j) { }
Arrays 17- 13 Array Elements can be Any Type
0 false boolean [] tests 1 false 2 false 3 true 0 1 Location [] line x y Location x y Location 5 2
Arrays 17- 15 Null Pointers 0 1 2 String [] words “I” “am” The default array value for Objects is null (the null pointer), which represents “no object”. String [] words2 = new String [3]; 0 1 2 String [] words words2[0] = “I”; words2[1] = “am”; words2[0] == null // evaluates to false words2[2] == null // evaluates to true; We depict variables holding the null pointer value as a box with a slash. Arrays 17- 16 NullPointerException Any attempt to send a message to the null pointer value results in a NullPointerException. words2[1].length() // evaluates to 2 // (length of string “am”, not of array) words2[2].length() // generates a NullPointerException NullPointerExceptions due to uninitialized variables are extremely common and will haunt you from now on. 0 1 2 String [] words “I” “am”
Arrays 17- 19 split() The split() instance method of the String class is useful for creating string arrays: String [ ] seuss = “I do not like green eggs and ham”.split(“ ”); StringArray.toString(seuss) // evaluates to “{I,do,not,like,green,eggs,and,ham}” Arrays 17- 20 Summing an Array 0 4 1 7 3 8 2 3 4 6 int[] nums // Assume this is in the class IntArray public static int sum (int [ ] a) { } Want IntArray.sum(nums) → 28 5 28
i result
Arrays 17- 21 Testing sum()
IntArray.sum(new int [] {4,7,3,8,6}) 28 Can test interactively in the Dr. Java Interaction Pane: Can include hardwired test arrays in a main() method: public static void main (String [] args) { String [ ] test = new int [ ] {4,7,3,8,6}; System.out.println(“sum(” + IntArray.toString(test)+ “) = ” + sum(test)); } Can read test arrays from the command line: public static void main (String [] args) { String [ ] test = new int [args.length]; for (int i = 0; i < args.length; i++) { test[i] = Integer.parseInt(args[i]); } System.out.println(“sum(” + IntArray.toString(test) + “) = ” + sum(test)); } Arrays 17- 22 Summing from Top Down 0 4 1 7 3 8 2 3 4 6 int[] nums // Assume this is in the class IntArray public static int sum (int [ ] a) { } Want IntArray.sum(nums) → 28 -1 28
i result
Arrays 17- 25 Nondestructive (Copying) Mapper 0 4 1 7 3 8 2 3 4 6 int[] nums // Assume this is in the class IntArray public static int[] mapDoubleCopy (int [] a) { }
0 4 1 7 3 8 2 3 4 6 int[] nums
0 8 1 14 3 16 2 6 4 12 Arrays 17- 26 Destructive Array Reversal 0 4 1 7 3 8 2 3 4 6 int[] nums // Assume this is in the class IntArray public static void reverse (int [] a) { }
0 6 1 8 3 7 2 3 4 4 int[]
Arrays 17- 27 A Broken Array Reversal
Arrays 17- 28 Nondestructive Array Reversal 0 4 1 7 3 8 2 3 4 6 int[] nums // Assume this is in the class IntArray public static int[] reverseCopy (int [] a) { }
0 4 1 7 3 8 2 3 4 6 int[] nums
0 6 1 8 3 7 2 3 4 4 int[] c
Arrays 17- 31
0 4 1 7 3 8 2 3 4 6 int[] array
Arrays 17- 32