Presentation is loading. Please wait.

Presentation is loading. Please wait.

©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 10 - 1 Chapter 10 Using arrays to create collections.

Similar presentations


Presentation on theme: "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 10 - 1 Chapter 10 Using arrays to create collections."— Presentation transcript:

1 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 10 - 1 Chapter 10 Using arrays to create collections ArrayList class

2 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 10 - 2 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

3 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 10 - 3 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) {…} …}

4 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 10 - 4 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++;

5 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 10 - 5 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

6 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 10 - 6 Java Interface A Java interface defines only the behavior of objects –It includes only public methods with no method bodies. –It does not include any data members except public constants –No instances of a Java interface can be created

7 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 10 - 7 JCF Lists JCF includes the List interface that supports methods to maintain a collection of objects as a linear list L = (l 0, l 1, l 2,..., l N ) 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.

8 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 10 - 8 List Methods Here are five of the 25 list methods: Returns the number of elements in the list int size ( ) Removes the element at position idx boolean remove ( int idx ) Returns the element at position idx Object get ( int idx ) Clears this list, i.e., make the list empty void clear ( ) Adds an object o to the list boolean add ( Object o )

9 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 10 - 9 Using Lists To use a list in a program, we must create an instance of a class that implements the List interface. Two classes that implement the List interface: –ArrayList –LinkedList The ArrayList class uses an array to manage data. The LinkedList class uses a technique called linked-node representation.

10 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 10 - 10 Sample List Usage Here's an example of manipulating a list: import java.util.*; List friends; Person person; friends = new ArrayList ( ); person = new Person("jane", 10, 'F'); friends.add( person ); person = new Person("jack", 6, 'M'); friends.add( person ); Person p = friends.get( 1 );

11 ©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 10 - 11 Using an Iterator The iterator method of the ArrayList class returns an object that you can use to loop through all the elements of the list Iterator iter = friends.iterator(); while (iter.hasNext() ) System.out.println( iter.next()); The next() method returns each object in the list in turn –the return type of next is Object so to use the Person methods, you have to cast the object to a person Person p = iter.next(); Use hasNext() to determine when all the elements of the list have been processed


Download ppt "©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter 10 - 1 Chapter 10 Using arrays to create collections."

Similar presentations


Ads by Google