Collections and Interfaces in Java Programming
Learn how to use arrays and Collections.ArrayList.class in creating collections, dynamic arrays, JCF lists, List methods, and interfaces for efficient Java programming.
Collections and Interfaces in Java Programming
E N D
Presentation Transcript
Using arrays to create collections ArrayList class Chapter 10
Collections • There are many examples of programs that need many objects of a particular type • A class roster needs many Student objects • A CD object needs many songs • A photo album needs many photos • We can use arrays for this • How do we deal with the fact that we don't know ahead of time how many objects there will be? • Some kinds of collections change size frequently
What does a Collection class look like? public class ItemCollection { private Item [] collection; private int size, capacity; public ItemCollection() {…} public void add( Item item) {…} public Item get( int index) {…} …}
Dynamic Arrays • Create the array with some standard size Item [] collection = new Item[capacity]; • Use a counter variable to keep track of the number of elements which are not null; • Each time you add an item, put it in the next available element and increment the counter. collection[size] = newItem; size++;
Dynamic Arrays • Before adding a new item, check to see if there is room • If the array is full, make a bigger array and copy the elements from the original if (size == capacity) Item [] newArray = new Item[2*capacity]; for (int i=0; i< size; i++) newArray[i] = collection[i]; collection = newArray; capacity *= 2; } // add element as above
JCF Lists • JCF includes two classes that support methods to maintain a collection of objects as a linear list L = (l0, l1, l2, . . . , lN) • We can add to, remove from, and retrieve objects in a given list. • A list does not have a set limit to the number of objects we can add to it.
boolean add(Objecto) Adds an object o to the list void clear( ) Clears this list, i.e., make the list empty Object get(int idx) Returns the element at position idx boolean remove(intidx) Removes the element at position idx int size( ) Returns the number of elements in the list List Methods • Here are five of the 25 list methods:
Using Lists • To use a list in a program, we must create an instance of one of the List classes. • ArrayList • LinkedList • The ArrayList class uses an array to manage data. • The LinkedList class uses a technique called linked-node representation.
import java.util.*; ArrayList<Person> friends; Person person; friends = new ArrayList<Person>( ); person = new Person("jane", 10, 'F'); friends.add( person ); person = new Person("jack", 6, 'M'); friends.add( person ); Person p = friends.get( 1 ); Sample List Usage • Here's an example of manipulating a list:
How to process the elements of a list? • Use a for loop for (int i=0; i<list.size(); i++) process( list.get(i)); • Use for in for (BaseType element : list) process( element); • Use an iterator
Using an Iterator • The iterator method of ArrayList returns an object you can use to loop through all the elements of the list Iterator<Person> iter = friends.iterator(); while (iter.hasNext()) System.out.println( iter.next()); • The next() method returns each element in turn Person p = iter.next(); • Use hasNext() to determine when all the elements of the list have been processed
Interfaces • Sometimes we have a number of classes that should have a common set of methods. • All lists should have methods like add, get, size, … • Interfaces provide a way to specify a set of methods that a group of classes should have in common. • A Java interface defines only behavior • It includes headers for public methods (no method bodies) • It does not include any data members except public constants • No instances of a Java interface can be created
Interfaces in Java • The Java libraries include many interfaces • There is a List interface that includes all the methods needed by lists • Both ArrayList and LinkedList implement List • They have the same methods List<Person> list; list = new ArrayList<Person>; list = new LinkedList<Person>; • The code works independent of which way you instantiated list • This is an example of polymorphism
Java Interfaces • Template for an interface public interface <interfaceName> { public <returnType> <methodName>( <paramList>); public final <constType> constName = <constValue>; }
Using an interface • Use the implements keyword header of a class that needs to have all the methods of the interface • Implement (provide code for) each method in the interface public class <implementingClassName> implements <interfaceName> { public <returnType> <methodName>( <paramList>){ // code for method } // add any other methods and data that is // appropriate for the class }
Using an interface type • You can use an interface type as the type for a reference (object) variable <interfaceName> <identifier>; • You have to instantiate the object using a class that implements the interface <identifier> = new <implementingClassName>(); • Now you can only call the methods that appear in the interface.