Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP 103 2014-T2 Lecture 3 Marcus Frean.

Slides:



Advertisements
Similar presentations
Why not just use Arrays? Java ArrayLists.
Advertisements

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with Java From Control Structures through Data Structures by.
CSC 205 – Java Programming II Lecture 25 March 8, 2002.
CSE 143 Lecture 22: Advanced List Implementation (ADTs; interfaces; abstract classes; inner classes; generics; iterators)
CS 106 Introduction to Computer Science I 05 / 03 / 2010 Instructor: Michael Eckmann.
Lists in Java Part of the Collections Framework. Kinds of Collections Collection --a group of objects, called elements –Set-- An unordered collection.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 10 Using arrays to create collections.
Building Java Programs Inner classes, generics, abstract classes reading: 9.6, 15.4,
CS 106 Introduction to Computer Science I 04 / 30 / 2010 Instructor: Michael Eckmann.
Arrays And ArrayLists - S. Kelly-Bootle
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Arrays COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
CS 106 Introduction to Computer Science I 04 / 25 / 2007 Instructor: Michael Eckmann.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
(c) University of Washington14-1 CSC 143 Java Collections.
ArrayList, Multidimensional Arrays
Lists Ellen Walker CPSC 201 Data Structures Hiram College.
Abstract Data Types. What’s on the menu? What’s an abstract data type? How do you implement it? ADT List.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Computer Science 209 Software Development Java Collections.
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.
(c) University of Washington15-1 CSC 143 Java List Implementation via Arrays Reading: 13.
ArrayList By Neil Butcher. What is the difference between an ArrayList and an Array? An ArrayList is in many ways similar to an array, but has a few subtle.
2014-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
CSE 143 Lecture 24 Advanced collection classes (ADTs; abstract classes; inner classes; generics; iterators) read 11.1, 9.6, , slides.
1 Collection, Iterable, and Iterator Interfaces The Collection Interface and its Hierarchy The Iterable and Iterator Interfaces For-each Loops with Iterable.
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.
2013-T2 Lecture 18 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Thomas Kuehne.
(c) University of Washington16-1 CSC 143 Java Linked Lists Reading: Ch. 20.
(c) University of Washington16-1 CSC 143 Java Lists via Links Reading: Ch. 23.
Lists Chapter 4. 2 Chapter Contents Specifications for the ADT List Redefining the Specifications Using the ADT List Using a List Is Like Using a Vending.
CSE 143 Lecture 20 Abstract classes. 2 Circle public class Circle { private double radius; public Circle(double radius) { this.radius = radius; } public.
1 CMPSCI 187 Computer Science 187 Introduction to Introduction to Programming with Data Structures Lecture 8 Lists, Iterators, and Doubly Linked Lists.
 In the java programming language, a keyword is one of 50 reserved words which have a predefined meaning in the language; because of this,
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists COMP 102 # T1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Types and Interfaces COMP.
Week 2 - Friday.  What did we talk about last time?  Computing Big Oh.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Thomas Kuehne.
Arrays and ArrayLists Topic 6. One Dimensional Arrays Homogeneous – all of the same type Contiguous – all elements are stored sequentially in memory For.
CSE 1201 Object Oriented Programming ArrayList 1.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
Iterators, Iterator, and Iterable 2015-T2 Lecture 8 School of Engineering and Computer Science, Victoria University of Wellington COMP 103 Thomas Kuehne.
Some Other Collections: Bags, Sets, Queues and Maps COMP T2 Lecture 4 School of Engineering and Computer Science, Victoria University of Wellington.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington Summary and Exam COMP 102.
Peter Andreae Computer Science Victoria University of Wellington Copyright: Peter Andreae, Victoria University of Wellington ArrayLists: varying size arrays.
IMPLEMENTING ARRAYLIST COMP 103. RECAP  Comparator and Comparable  Brief look at Exceptions TODAY  Abstract Classes - but note that the details are.
JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP T2, Lecture 2 Marcus Frean.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. Chapter Chapter 18 List ADT Animated Version.
2015-T2 Lecture 19 School of Engineering and Computer Science, Victoria University of Wellington  Marcus Frean, Lindsay Groves, Peter Andreae, and John.
Week 6 - Friday.  What did we talk about last time?  Loop examples.
Implementing ArrayList Part T2 Lecture 6 School of Engineering and Computer Science, Victoria University of Wellington  Thomas Kuehne, Marcus Frean,
CS-2852 Data Structures Week 4, Class 1 - Review Review! Thursday Exam I - Review Implementing ArrayList Big-O Iterators – High-level description Linked.
Quiz: Design a Product Class Create a definition for a class called Product, which keeps track of the following information: –Name of the product –Weight.
Chapter 9 Introduction to Arrays Fundamentals of Java.
 2016, Marcus Biel, ArrayList Marcus Biel, Software Craftsman
1 CS162: Introduction to Computer Science II Abstract Data Types.
Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Thomas Kuehne.
Using the Java Collection Libraries COMP 103 # T2
Collections COMP 103 #3 2008T2.
JAVA COLLECTIONS LIBRARY
A tree set Our SearchTree class is essentially a set.
Implementing ArrayList Part 1
CSE 143 Lecture 27: Advanced List Implementation
Lecture 26: Advanced List Implementation
A tree set Our SearchTree class is essentially a set.
ArrayLists 22-Feb-19.
slides created by Alyssa Harding
CSE 143 Lecture 21 Advanced List Implementation
Presentation transcript:

Interfaces, Classes, Collections School of Engineering and Computer Science, Victoria University of Wellington COMP T2 Lecture 3 Marcus Frean

RECAP - TODAY 2 RECAP  Libraries, Collections: the Java Collections Library  Interfaces, Abstract Data Types TODAY  More on interface versus class, and the Collections library  Parameterized Types ADMINISTRIVIA  Assignment#1 will go out next Monday  Videos should appear on “blackboard” from now on  Tutorials start Next Week. Can’t sign up yet sorry. I will everyone once you can.

3 QUICK NOTE: Code style  I will drop the “ this. ” except when needed  Instead of this.loadFromFile(...) just loadFromFile(...) Instead of this.shapes.addShape(shape) just shapes.addShape(shape)  I will leave out { } when surrounding just one statement  Instead of while(i<name.length) { name[i] = null; } just while(i<name.length) name[i] = null;

InterfaceClasses  enables us to say “all shapes must be able to draw themselves”  enables us to declare a List of Shapes, without saying exactly what each shape is going to be. 4 Java Interfaces Rect Shape Triange implements Oval implements public interface Shape { public void redraw(); : } public interface Shape { public void redraw(); : } public class Rect implements Shape { public void redraw() { UI.drawRect(x,y,blah,blah). } public class Rect implements Shape { public void redraw() { UI.drawRect(x,y,blah,blah). }

InterfacesClasses Interfaces can extend other interfaces (the “sub” interface has all the methods of the “super” interface, plus its own methods) 5 Java Interfaces (for collections) ArrayList Collection List LinkedList extends implements

extends implements an interface: says what you’re doing an implementation (=a class): how you’re doing it And there’s a hierarchy to interfaces, so we say they “extend” one another

Parameterised Types 7  The structure and access discipline of a collection is the same, regardless of the type of value in it:  A set of Strings, a set of Persons, a set of Shapes, a set of integers all behave the same way. ⇒ we only want one Interface for each kind of collection. (there is only one Set interface)  Need to specify kind of elements in a particular collection ⇒ The collection Interfaces (and classes) are parameterised:  Interface has a “generic” type parameter  When declaring a variable collection, you specify the type of the collection and the actual type of the elements of the collection

 In the Interface, we use a placeholder: public interface Set extends Collection { public void add(E item); /*…description…*/ public void remove(E item); /*…description…*/ public boolean contains(E item);/*…description…*/ … // (lots more methods in the Java Set interface)  When declaring variable, specify the actual type of element private Set mystuff; private List drawing; Parameterised Types 8 Collection Type Type of value in Collection

Two ways to keep a list (assuming we have a class called Thing)  as an Array: private static final int maxThings = 1000; private Thing[ ] mystuff = new Thing[maxThings]; private int count = 0;  as a List (interface), implemented via an ArrayList (class) : private List mystuff = new ArrayList (); 9 9 Using ArrayList: declaring

 Your program can declare a variable, parameter, or field of the interface type private List myThings; // a list of Thing objects 2. instantiate an object: myThings = new ArrayList (); 3. call methods on that variable, parameter, or field myThings.add(new Thing( “discombobulator”, “yellow”, “huge”)); myThings.get(3).toString(); Using Java Collection Interfaces 10 Aside: this won ’ t work. Why not?

Since interfaces form a hierarchy, you can do this too: 1. declare a variable, parameter, or field of the interface type, as in private List drawing; // a list of Shape objects 2. instantiate an object, as in drawing= new ArrayList (); 3. call methods on that variable, parameter, or field, as in drawing.add(new Rect(100, 100, 20, 30));...so long as the Rect class implements the Shape interface: public class Rect implements Shape {..... Interesting aspect (or rather problem!): how can we specify the type of the values in the List interface code ? Another (interesting) example 11

Collection isEmpty() → boolean size() → int contains(E elem)→ boolean add(E elem) → boolean (whether it succeeded) remove(E elem) → boolean (whether it removed an item) iterator() → iterator … List add(int index, E elem) remove(int index) → E (returns the item removed) get(int index) → E set(int index, E elem) → E (returns the item replaced) indexOf(E elem)→ int subList(int from, int to)→ List … 12 Methods on Collection and List Methods on all types of collections Additional methods on all Lists

 Wanted: a collection of tasks, in order they should be done.  Collection type: List  Element type: Task  Requirements of TasksOrganiser:  read list of tasks from a file  display all the tasks  add task, at end, or at specified position  remove task  move task to a different position. 13 Example

14 Example (TasksOrganiser program) public class TasksOrganiser { private List tasks; /* read a list of tasks from a file, */ public void readTasks(String fname){ try { Scanner sc = new Scanner(new File(fname)); tasks = new ArrayList (); while ( sc.hasNext() ) tasks.add(new Task(sc.next())); sc.close(); } catch(IOException e){…} displayTasks(); }...

public void displayTasks(){ UI.print(tasks.size() + “ tasks to be done:\n”); // You could do it this way (uses the standard “for” loop) for (int i=0; i<tasks.size(); i++) UI.print(tasks.get(i) + "\n"); // or this way instead (uses the “for each” syntax) for (Task task : tasks) UI.print(task + "\n"); // There is even a third way! (use an iterator) Iterator iter = tasks.iterator(); while (iter.hasNext()){ UI.print(iter.next() + "\n"); } 15 Iterating through List “Er… what?” (This is coming up)

 No size limit!!! They grow bigger as necessary  Lots of code written for you List vs Array taskList.set(ind, value) taskArray[ind] = value taskList.get(ind)taskArray[ind] taskList.size()? Not same as length of the array! taskList.add(value)? Where is the last value? What happens if it’s full? taskList.add(ind, value)? Have to shift everything up!! taskList.remove(ind)? Have to shift everything down!! taskList.remove(value)? Have to find value, then shift things down!! for (Task t : tasks) for( int i = 0; i< ???; i++) Task t = taskArray[ i ]; 16 Lists are nicer than arrays