slides created by Ethan Apter

Slides:



Advertisements
Similar presentations
CSE 143 Lecture 17 Binary Search Trees and Comparable slides created by Ethan Apter
Advertisements

Using ArrayList. Lecture Objectives To understand the foundations behind the ArrayList class Explore some of the methods of the ArrayList class.
CSE 143 Lecture 3 Inheritance slides created by Marty Stepp
Programming With Java ICS201 University Of Ha’il1 Chapter 14 Generics and The ArrayList Class.
ARRAYLIST.. Hazen High School. Vocabulary to Know ArrayList Generic Class ArrayList Operations ArrayList Methods ArrayList Searching For-Each Wrapper.
ArrayList, Multidimensional Arrays
CSE 143 Lecture 2 More ArrayList ; classes and objects reading: 10.1; slides created by Marty Stepp and Hélène Martin
5-Aug-2002cse Arrays © 2002 University of Washington1 Arrays CSE 142, Summer 2002 Computer Programming 1
CSE 143 Lecture 4 ArrayList Reading: 10.1 slides created by Marty Stepp
ArrayList Class An ArrayList is an object that contains a sequence of elements that are ordered by position. An ArrayList is an object that contains a.
Aug 9, CMSC 202 ArrayList. Aug 9, What’s an Array List ArrayList is  a class in the standard Java libraries that can hold any type of object.
CSE 1201 Object Oriented Programming ArrayList 1.
CSE 143 Lecture 4 More ArrayIntList : Pre/postconditions; exceptions; testing reading: slides created by Marty Stepp and Hélène Martin
CSE 143 Lecture 3 Implementing ArrayIntList reading: slides created by Marty Stepp and Hélène Martin
Coming up Implementation vs. Interface The Truth about variables Comparing strings HashMaps.
CMSC 202 ArrayList Aug 9, 2007.
Chapter VII: Arrays.
CSE 143 Lecture 14 Interfaces; Abstract Data Types (ADTs)
Sixth Lecture ArrayList Abstract Class and Interface
Building Java Programs
(like an array on steroids)
CSE 143 Lecture 3 More ArrayList; object-oriented programming
Building Java Programs
Building Java Programs
March 29th Odds & Ends CS 239.
Lecture 2: Implementing ArrayIntList reading:
Building Java Programs
Building Java Programs
CSE 143 Lecture 4 ArrayList Reading: 10.1.
slides created by Marty Stepp and Ethan Apter
ArrayLists.
CS 302 Week 8 Jim Williams, PhD.
slides created by Ethan Apter
CS Week 9 Jim Williams, PhD.
Building Java Programs
CS 200 Objects and ArrayList
CMSC 202 ArrayList Aug 9, 2007.
ArrayLists.
CSE 143 Lecture 2 Collections and ArrayIntList
Lecture 2: Implementing ArrayIntList reading:
slides created by Ethan Apter
Welcome to CSE 143! Go to pollev.com/cse143.
CMSC 202 ArrayList Aug 9, 2007.
Lecture 13: ArrayLists AP Computer Science Principles
CSE 143 Lecture 4 Implementing ArrayIntList
slides created by Marty Stepp
CSE 143 Lecture 5 References and Linked Nodes
CS 200 Objects and ArrayList
Go to pollev.com/cse143.
CSE 143 Lecture 3 Implementing ArrayIntList reading:
CSE 143 Lecture 4 Implementing ArrayIntList; Binary Search
slides created by Marty Stepp
Building Java Programs
slides created by Alyssa Harding
Linked Lists based on slides created by Ethan Apter
CSE 143 Lecture 2 ArrayIntList, binary search
Generics, Stack, Queue Based on slides by Alyssa Harding
slides created by Ethan Apter
Building Java Programs
slides created by Marty Stepp
CSE 143 Lecture 3 More ArrayList; object-oriented programming
Lecture 7: Linked List Basics reading: 16.2
CSE 143 Lecture 4 Implementing ArrayIntList reading:
slides created by Ethan Apter and Marty Stepp
Linked Lists and Loops based on slides by Ethan Apter
CSE 143 Lecture 21 Advanced List Implementation
slides created by Ethan Apter
CS 200 Objects and ArrayList
ArrayLists Readings: 10.1 (pg. 559 – 571).
Implementing ArrayIntList
Presentation transcript:

slides created by Ethan Apter CSE 143 Lecture 2 ArrayIntList slides created by Ethan Apter http://www.cs.washington.edu/143/

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

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

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

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

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;

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; }

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 } Updated We need to add values to the ArrayIntList

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

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

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

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

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++; }

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 } Updated We still need to print our lists

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

print 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("]");

print 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

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

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?

print 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 + "]”;

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

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

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