// APS101, Winter 2009: Lecture 25 (Mar. 12) // // Review: last time we kept looking at the properties of one-dimensional arrays. // we wrote method findElement in ArrayDemo.java // what if we want to copy the contents of one array into another? int[] a = {1, 2, 3}; int[] b = a; // ...can't do this, because if you change a, then b changes (and if you change b, then a changes) // more technically: you're making a and b refer to the same object (an array of ints) // so, let's write a method to copy an array and return a new one: ArrayDemo.copyArray(new int[]{1,2,3}, 6) ArrayDemo.copyArray(new int[]{1,2,3}, 6)[0] ArrayDemo.displayArray(ArrayDemo.copyArray(new int[]{1,2,3}, 6), 0, 5) int[] a = {1, 2, 3}; int[] b = ArrayDemo.copyArray(new int[]{1,2,3}, 3) // now, if you change one of the arrays, the other is not affected. // how about if we want to check if two arrays are equal? int[] a = {1, 2, 3}; int[] b = {1, 2, 3}; a == b // this doesn't work... why? a.equals(b) // this also doesn't work // need to write our own equals method in ArrayDemo.java int[] a; int[] b; ArrayDemo.equals(a, b) // returns true, because both arrays are null int[] b = {1, 2, 3}; ArrayDemo.equals(a, b) // returns false, because only one array is null int[] a = {1, 2, 3, 4}; ArrayDemo.equals(a, b) // returns false, because the length of the arrays is different int[] a = {1, 2, 4}; ArrayDemo.equals(a, b) // returns false, because the content of the arrays is different int[] a = {1, 2, 3}; ArrayDemo.equals(a, b) // returns true, because both arrays have the same content // what if we want to swap two elements in an array? // ex. {1, 3, 2} -> {1, 2, 3} int[] a = {1, 3, 2}; int temp = a[1]; // store the 3 a[1] = a[2]; // put the 2 in the previous position a[2] = temp; // put the stored 3 in the last position // now, let's write a swap method in ArrayDemo.java ArrayDemo.swap(new int[]{1, 3, 2}, 0, 1) ArrayDemo.swap(new int[]{1, 3, 2}, 0, 0) // nothing happens! int[] a = {1, 3, 2}; int[] b = ArrayDemo.swap(a, 0, 1); ArrayDemo.displayArray(b, 0, 2)) ArrayDemo.displayArray(a, 0, 2)) // a has changed as well! a == b // a and b are the same object... why? // look at swap: it does the swapping "in place" without creating a new array, so it returns the original reference to a. // Exercise: change swap such that it doesn't change the original array, and returns the result as a new array. // let's apply our knowledge of 1-D arrays to write a class called Pizza.java: Pizza p = new Pizza(3, "large"); p // IMPORTANT: if the object has a toString() method, it is automatically called if you just write the variable name! p.toString() // this does the same thing as the above statement. p.addTopping("pepperoni"); p p.addTopping("extra cheese"); p p.addTopping("olives"); p p.addTopping("anchovies"); p // this topping wasn't added! why? Pizza p = new Pizza(2, "small"); p.addTopping("ham"); p.addTopping("ham"); p // unfortunately, putting in the same topping more than once works... why? (look at the code) // Exercise: write extra code that will check for (and prevent) duplicate toppings