Presentation is loading. Please wait.

Presentation is loading. Please wait.

JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP 103 2014-T2, Lecture 2 Marcus Frean.

Similar presentations


Presentation on theme: "JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP 103 2014-T2, Lecture 2 Marcus Frean."— Presentation transcript:

1 JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP 103 2014-T2, Lecture 2 Marcus Frean

2 TODAY 2  Libraries and code reuse  Java’s Collections Library  ArrayList (our first example)  Interfaces and Classes Administrivia:  Yesterday’s course outline had two typos:  Test will be on August 21 st (not 9 th )  Exam will be 2 hours long (not 3)

3 3 Programming with Libraries Modern programs (especially GUI and network) are too big to build from scratch, so we have to reuse code written by other people....  Libraries contain code designed for reuse  Java has a huge number of standard libraries... Packages, which are groups of Classes  The ecs100 library has some very useful classes (eg. UI)  There are LOTS of other libraries as well  Learning to use libraries is ESSENTIAL Java API

4 4 Libraries for COMP103  ecs100Special classes for text/graphical input and output, especially for GUI  java.utilthe Collection classes, and other utility classes  java.ioClasses for input and output  javax.swingLarge libraries of classes for GUI jawa.awt programs We will use libraries in almost every program

5  Read the documentation to pick useful library  Import the package or class into your program import java.util.*; import ecs100.*;  Read the documentation to identify how to use  Constructors for making instances  Methods that you can call  Interfaces that you can implement  Use the classes as if they were part of your program 5 Using Libraries Java API

6 6 “Standard” Collections  Common ways of organizing a collection of values:  Each of these is a different type of collection some collections... BagGraphTreeStackQueueListMapSet

7 Java Collections Library  Standard collections (eg. Set, List, Map, etc.) are implemented for you  Java Collections Library  Code written by others – for you to use  Collections specified as interfaces Collection, Set, List, Queue – are all interfaces Using collections implies using one of the many concrete classes that implement these interfaces. Eg. You can use: ArrayList (a class) which implements List (an interface).... 7

8 8 8 You have been using ArrayList  Part of the Java Collections framework  Stores a LIST of items a collection of items kept in a particular order  Part of the java.util package ⇒ need import java.util.*; at the head of your file  You can make a new ArrayList object, and put items in it  Don’t have to specify its size: Like an infinitely stretchable array  Should specify the type of items  But, you can’t use the [...] notation  You have to call methods to access and assign

9 List of students (assume we have a class called Student )  Array: private static final int maxStudents = 1000; private Student[ ] students = new Student[maxStudents]; private int count = 0;  ArrayList: private ArrayList students = new ArrayList ();  Type of elements in the list is between “ ” after ArrayList  No maximum, no initial size (!), no explicit count 9 9 Using ArrayList: declaring

10 10 Using ArrayList: methods ArrayList has many methods!  size(): returns the number of items in the list  add(item): adds an item to the end of the list  add(index, item): inserts an item at index (moves later items up)  set(index, item): replaces the item at index with item  contains(item): true if the list contains an item that equals item  get(index): returns the item at position index  remove(item): removes an occurrence of item  remove(index): removes the item at position index (both “removes” move the later items down)  You can use the “for each” loop on an ArrayList, as well as for loop TIP: Read Documentation from sidebar of course homepage

11 11 Using ArrayList private List students = new ArrayList (); Student s = new Student(“Davy Jones”, 300012345); students.add(s); for(Student st: students) UI.println(st.toString()); for(int i = students.size()-1; i>=0; i--) UI.println(students.get(i).toString()); if(students.contains(current)) { UI.println(current); students.remove(current); }....

12 12 Collections  An object that serves as a repository for other objects (like a “container”)  Type of Elements (a collection of ….)  Constraints (duplicates ? Access: add anywhere, remove from one end only, get from top, etc…)  Structure (no/linear/hierarchical)

13 13 Example  Bag : a example of a collection  Type of Elements Bag of Students  Structure No structure/order  Constraints Duplicates allowed, add/remove anywhere (no order) Collection type Element type

14 14 Is ArrayList a Collection type then?  Type of Elements ArrayList of Students  Structure linear order  Constraints Duplicates allowed, add/remove anywhere Collection type Element type actually no! ArrayList is not a Collection type List is! ArrayList is just ONE WAY to “do” (implement) a List

15 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

16 Interfaces :  Collection most general: “a bag”  List ordered collection  Set unordered, no duplicates  Queue ordered collection with limited access (add at one end, remove from other)  Map key-value pairs (or mapping) Specify the Types Classes:  List classes: ArrayList, LinkedList, Vector  Set classes: HashSet, TreeSet, EnumSet, LinkedHashSet,…  Map classes: EnumMap, HashMap, TreeMap, LinkedHashMap, WeakHashMap, … …… Implement the interfaces Java Collections Library 16 abstract concrete advantages and disadvantages

17 Java Interfaces 17  At the simplest level:  An interface is a bunch of method signatures with a name. (Method signature only, no bodies)  At a higher level:  An interface is an abstract concept that defines: the operations that can be done to an object of this type how it will behave  No concrete details!  No constructors- can’t make an instance  No fields- doesn’t say how to store the data  No method bodies.- doesn’t say how to perform the operations  Details provided by Classes that implement the Interface

18 18 Abstract Data Types (ADT)  an ADT is a type of data, described at an abstract level: Specifies the operations that can be done to an object of this type Outlines how it will behave  A Java Interface corresponds to an Abstract Data Type Specifies what methods can be called on objects of this type (specifies name, parameters and types, and type of return value) Behaviour of methods is only given in comments (but cannot be enforced)  No constructors- can’t make an instance: new Set()  No fields- doesn’t say how to store the data  No method bodies.- doesn’t say how to perform the operations public interface Set { public void add( ??? item); /*…description…*/ public void remove( ??? item); /*…description…*/ public boolean contains( ??? item);/*…description…*/ … // (plus lots more methods in the Java Set interface)

19  Earlier, we declared as ArrayList like this: private ArrayList students = new ArrayList ();  BETTER / NOW: declare as an instance of the interface List: private List students = new ArrayList (); 19 declaring a List

20  Libraries  Collections  Interfaces – correspond to ADTs. No concrete details.  Eg. Collection, List, Set…  ArrayList – example of a class that implements an interface (List) from the Java Collections Library. Summary 20

21  How do we define the type of element a collection contains ?  Constructing a new object of an appropriate collection class.  What can we do with them ?  What methods can we call on them ?  How do we iterate down all the elements of a collection ?  How do we choose the right collection interface and class ?  What if there isn’t the right class for what we want ? What’s Next ? 21


Download ppt "JAVA COLLECTIONS LIBRARY School of Engineering and Computer Science, Victoria University of Wellington COMP 103 2014-T2, Lecture 2 Marcus Frean."

Similar presentations


Ads by Google