CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java...
Transcript of CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java...
![Page 1: CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java Collections Framework • ... • Read chapter 10.1 or Java API for more information .](https://reader031.fdocuments.net/reader031/viewer/2022041401/5e1779d069e23e1fd519fe0e/html5/thumbnails/1.jpg)
CSE 143 Lecture 2
ArrayIntList
slides created by Ethan Apter http://www.cs.washington.edu/143/
![Page 2: CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java Collections Framework • ... • Read chapter 10.1 or Java API for more information .](https://reader031.fdocuments.net/reader031/viewer/2022041401/5e1779d069e23e1fd519fe0e/html5/thumbnails/2.jpg)
2
ArrayList Review • ArrayList is an Object
– It is one of the lists within the Java Collections Framework
• ArrayLists can store any kind of Object – String, Integer, Scanner, etc – …but they can’t store primitives like int and double
• ArrayLists can perform many useful list operations – add(value) // adds given value to end of list – get(index) // returns value at given index – size() // returns size of list – etc
![Page 3: CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java Collections Framework • ... • Read chapter 10.1 or Java API for more information .](https://reader031.fdocuments.net/reader031/viewer/2022041401/5e1779d069e23e1fd519fe0e/html5/thumbnails/3.jpg)
3
ArrayList Review • Sample ArrayList code:
import java.util.*; // must occur before using ArrayList
// construct new ArrayList of Strings ArrayList<String> list = new ArrayList<String>();
// add a few elements, and print list.add(“Apter”); // adds “Apter” to end list.add(0, “Ethan”); // adds “Ethan” to index 0, shifting others System.out.println(list); // prints [Ethan, Apter]
// remove an element, and print list.remove(0); // removes “Ethan” and shifts others System.out.println(list); // prints [Apter]
• Read chapter 10.1 or Java API for more information
![Page 4: CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java Collections Framework • ... • Read chapter 10.1 or Java API for more information .](https://reader031.fdocuments.net/reader031/viewer/2022041401/5e1779d069e23e1fd519fe0e/html5/thumbnails/4.jpg)
4
Learning from ArrayList
• We’re going to build our own version of ArrayList
• However, the full version of ArrayList is complicated – it can store any kind of Object – it has more than 20 methods
• So we will make a simpler version called ArrayIntList – it will store only one kind of thing (ints) – it will have fewer methods – it will improve our understanding of arrays and Objects
![Page 5: CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java Collections Framework • ... • Read chapter 10.1 or Java API for more information .](https://reader031.fdocuments.net/reader031/viewer/2022041401/5e1779d069e23e1fd519fe0e/html5/thumbnails/5.jpg)
5
ArrayIntList Client Code • First we need some client code:
public class ArrayIntListClient { public static void main(String[] args) { // construct ArrayIntList list1 // construct ArrayIntList list2 // add 6, 43, 97 to list1 // add 72, -8 to list2 // print list1 // print list2 } }
We need to make an ArrayIntList class to construct
![Page 6: CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java Collections Framework • ... • Read chapter 10.1 or Java API for more information .](https://reader031.fdocuments.net/reader031/viewer/2022041401/5e1779d069e23e1fd519fe0e/html5/thumbnails/6.jpg)
6
ArrayIntList
• What variables should be in ArrayIntList? – an array, to store the ints – an int, to store the size
• The size variable allows us to distinguish between our list’s capacity (the amount the array can hold) and our list’s size (the amount of valid data in the array)
• Variable code int[] elementData = new int[100]; int size = 0;
![Page 7: CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java Collections Framework • ... • Read chapter 10.1 or Java API for more information .](https://reader031.fdocuments.net/reader031/viewer/2022041401/5e1779d069e23e1fd519fe0e/html5/thumbnails/7.jpg)
7
ArrayIntList
• We don’t want to force the client to make these two variables
• Instead, we’ll encapsulate the variables in an Object
public class ArrayIntList { int[] elementData = new int[100]; int size = 0; }
![Page 8: CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java Collections Framework • ... • Read chapter 10.1 or Java API for more information .](https://reader031.fdocuments.net/reader031/viewer/2022041401/5e1779d069e23e1fd519fe0e/html5/thumbnails/8.jpg)
8
ArrayIntList Client Code • Now we can update our client code to construct ArrayIntLists:
public class ArrayIntListClient { public static void main(String[] args) { ArrayIntList list1 = new ArrayIntList(); ArrayIntList list2 = new ArrayIntList(); // add 6, 43, 97 to list1 // add 72, -8 to list2 // print list1 // print list2 } }
We need to add values to the ArrayIntList
Updated
![Page 9: CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java Collections Framework • ... • Read chapter 10.1 or Java API for more information .](https://reader031.fdocuments.net/reader031/viewer/2022041401/5e1779d069e23e1fd519fe0e/html5/thumbnails/9.jpg)
9
ArrayIntList Client Code: Attempt
• We might try something like this:
public class ArrayIntListClient { public static void main(String[] args) { ArrayIntList list1 = new ArrayIntList(); ArrayIntList list2 = new ArrayIntList(); // add 6, 43, 97 to list1 list1.elementData[0] = 6; list1.elementData[1] = 43; list1.elementData[2] = 97; list1.size = 3; // add 72, -8 to list2 // print list1 // print list2 } }
It works, but it’s not an object-oriented approach
![Page 10: CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java Collections Framework • ... • Read chapter 10.1 or Java API for more information .](https://reader031.fdocuments.net/reader031/viewer/2022041401/5e1779d069e23e1fd519fe0e/html5/thumbnails/10.jpg)
10
ArrayIntList
• Instead, we want to write an add method
• The code within the add method will look like this: elementData[size] = value; size++;
• The parameters for the add method will be: value
• But what about elementData and size? • Our add method will be an instance method instead of a static method • It will have an implicit parameter containing elementData and size
![Page 11: CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java Collections Framework • ... • Read chapter 10.1 or Java API for more information .](https://reader031.fdocuments.net/reader031/viewer/2022041401/5e1779d069e23e1fd519fe0e/html5/thumbnails/11.jpg)
11
Static and Instance Methods • A static add method would look something like this:
public static void add(int value) { // defines a static method add … }
• While an instance add method would look like this:
public void add(int value) { // defines an instance method add … }
• Notice the lack of the word static in the instance method
![Page 12: CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java Collections Framework • ... • Read chapter 10.1 or Java API for more information .](https://reader031.fdocuments.net/reader031/viewer/2022041401/5e1779d069e23e1fd519fe0e/html5/thumbnails/12.jpg)
12
Static and Instance Methods • Static method call: add(13); // call on static method add
• However, instance methods require dot notation
• Instance method calls: list1.add(1); // call on list1’s instance method add list2.add(7); // call on list2’s instance method add
• The variable before the dot (list1 and list2 above) is the implicit parameter
![Page 13: CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java Collections Framework • ... • Read chapter 10.1 or Java API for more information .](https://reader031.fdocuments.net/reader031/viewer/2022041401/5e1779d069e23e1fd519fe0e/html5/thumbnails/13.jpg)
13
ArrayIntList
• So our updated ArrayIntList, containing an instance add method, looks like this:
public class ArrayIntList { int[] elementData = new int[100]; int size = 0;
public void add(int value) { elementData[size] = value; size++; } }
![Page 14: CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java Collections Framework • ... • Read chapter 10.1 or Java API for more information .](https://reader031.fdocuments.net/reader031/viewer/2022041401/5e1779d069e23e1fd519fe0e/html5/thumbnails/14.jpg)
14
ArrayIntList Client Code • Now we can update our client code to add values to the ArrayIntLists: public class ArrayIntListClient { public static void main(String[] args) { ArrayIntList list1 = new ArrayIntList(); ArrayIntList list2 = new ArrayIntList(); list1.add(6); list1.add(43); list1.add(97); list2.add(72); list2.add(-8);
// print list1 // print list2 } }
We still need to print our lists
Updated
![Page 15: CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java Collections Framework • ... • Read chapter 10.1 or Java API for more information .](https://reader031.fdocuments.net/reader031/viewer/2022041401/5e1779d069e23e1fd519fe0e/html5/thumbnails/15.jpg)
15
But Does add Work? • One quick way to check is to print just the sizes of the lists: public class ArrayIntListClient { public static void main(String[] args) { ArrayIntList list1 = new ArrayIntList(); ArrayIntList list2 = new ArrayIntList(); list1.add(6); list1.add(43); list1.add(97); list2.add(72); list2.add(-8); System.out.println(list1.size); System.out.println(list2.size); } }
• Fortunately, we can do even better with jGRASP
Updated
![Page 16: CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java Collections Framework • ... • Read chapter 10.1 or Java API for more information .](https://reader031.fdocuments.net/reader031/viewer/2022041401/5e1779d069e23e1fd519fe0e/html5/thumbnails/16.jpg)
16
• This is the print method from yesterday’s section:
public static void print(int[] list) { if (list.length == 0) { System.out.println("[]"); } else { System.out.print("[" + list[0]); for (int i = 1; i < list.length; i++) { System.out.print(", " + list[i]); } System.out.println("]"); } }
![Page 17: CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java Collections Framework • ... • Read chapter 10.1 or Java API for more information .](https://reader031.fdocuments.net/reader031/viewer/2022041401/5e1779d069e23e1fd519fe0e/html5/thumbnails/17.jpg)
17
• Let’s update print to be an instance method:
public void print() { if (size == 0) { System.out.println("[]"); } else { System.out.print("[" + elementData[0]); for (int i = 1; i < size; i++) { System.out.print(", " + elementData[i]); } System.out.println("]"); } }
• Revisions: removed static, removed parameter, changed list to elementData, changed list.length to size
![Page 18: CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java Collections Framework • ... • Read chapter 10.1 or Java API for more information .](https://reader031.fdocuments.net/reader031/viewer/2022041401/5e1779d069e23e1fd519fe0e/html5/thumbnails/18.jpg)
18
ArrayIntList Client Code • Now we can update our client code to use the print method:
public class ArrayIntListClient { public static void main(String[] args) { ArrayIntList list1 = new ArrayIntList(); ArrayIntList list2 = new ArrayIntList(); list1.add(6); list1.add(43); list1.add(97); list2.add(72); list2.add(-8); list1.print(); list2.print(); } }
Updated
![Page 19: CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java Collections Framework • ... • Read chapter 10.1 or Java API for more information .](https://reader031.fdocuments.net/reader031/viewer/2022041401/5e1779d069e23e1fd519fe0e/html5/thumbnails/19.jpg)
19
print’s Limitations • print forces us to always print to System.out
– What if we want to send it to a file instead? – What if we’re working on a Graphical User Interface (GUI)
and we want the output to appear on a specific part of the screen?
• print forces us to print only a single ArrayIntList per line – What if we want two or more on the same line?
• If the client wants to do either of the above, she will have to handle the details herself. That’s not good design on our part.
• How can we fix this?
![Page 20: CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java Collections Framework • ... • Read chapter 10.1 or Java API for more information .](https://reader031.fdocuments.net/reader031/viewer/2022041401/5e1779d069e23e1fd519fe0e/html5/thumbnails/20.jpg)
20
• Let’s update print to return a String:
public String print() { if (size == 0) { return "[]”; } else { String result = "[" + elementData[0]; for (int i = 1; i < size; i++) { result += ", " + elementData[i]; } return result + "]”; } }
![Page 21: CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java Collections Framework • ... • Read chapter 10.1 or Java API for more information .](https://reader031.fdocuments.net/reader031/viewer/2022041401/5e1779d069e23e1fd519fe0e/html5/thumbnails/21.jpg)
21
ArrayIntList Client Code • Now we can update our client code to use the updated print
method: public class ArrayIntListClient { public static void main(String[] args) { ArrayIntList list1 = new ArrayIntList(); ArrayIntList list2 = new ArrayIntList(); list1.add(6); list1.add(43); list1.add(97); list2.add(72); list2.add(-8); System.out.println(list1.print()); System.out.println(list2.print()); } }
• But this doesn’t seem like much of an improvement
Updated
![Page 22: CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java Collections Framework • ... • Read chapter 10.1 or Java API for more information .](https://reader031.fdocuments.net/reader031/viewer/2022041401/5e1779d069e23e1fd519fe0e/html5/thumbnails/22.jpg)
22
toString • Let’s rename print to toString:
public String toString() { if (size == 0) { return "[]”; } else { String result = "[" + elementData[0]; for (int i = 1; i < size; i++) { result += ", " + elementData[i]; } return result + "]”; } }
• Java will automatically call toString when we pass our ArrayIntList to System.out.println
![Page 23: CSE 143 Lecture 2 - courses.cs.washington.edu file– It is one of the lists within the Java Collections Framework • ... • Read chapter 10.1 or Java API for more information .](https://reader031.fdocuments.net/reader031/viewer/2022041401/5e1779d069e23e1fd519fe0e/html5/thumbnails/23.jpg)
23
ArrayIntList Client Code • Now we can update our client code to use our toString
method:
public class ArrayIntListClient { public static void main(String[] args) { ArrayIntList list1 = new ArrayIntList(); ArrayIntList list2 = new ArrayIntList(); list1.add(6); list1.add(43); list1.add(97); list2.add(72); list2.add(-8); System.out.println(list1); System.out.println(list2); } }
Updated