Presentation is loading. Please wait.

Presentation is loading. Please wait.

ArrayLists.  The Java API includes a class named ArrayList. The introduction to the ArrayList class in the Java API documentation is shown below.  java.lang.Object.

Similar presentations


Presentation on theme: "ArrayLists.  The Java API includes a class named ArrayList. The introduction to the ArrayList class in the Java API documentation is shown below.  java.lang.Object."— Presentation transcript:

1 ArrayLists

2  The Java API includes a class named ArrayList. The introduction to the ArrayList class in the Java API documentation is shown below.  java.lang.Object ◦ java.util.AbstractCollection  java.util.AbstractList  java.util.ArrayList

3  The ArrayList class is sort of a hybrid. As its name implies, it has array-like characteristics, as well as characteristics that can be described as list-like.  It has a wide variety of methods that allow you to manipulate its elements in many different ways.

4  In the interests of brevity, none of these methods are presented here.  Some will be used in the example code which will follow. If necessary, you should take a look in the Java API documentation to see what methods are available and to get information on the use of specific methods.

5  In order to use an ArrayList in a program it has to be imported with the following statement: ◦ import java.util.ArrayList;

6  The declaration of an ArrayList can use angle bracket notation to specify what kinds of elements it contains. For example, the following line of code in a program would declare and construct an instance of an ArrayList with the name myCollection that contained elements which were references to instances of the Cup7 class: ◦ ArrayList myCollection = new ArrayList ();

7  If it is desirable for the ArrayList to contain simple types like integers or doubles, the values should be stored in instances of the wrapper classes Integer and Double, and references to these objects can be elements of the ArrayList. ◦ ArrayList yourCollection = new ArrayList (); ◦ It’s using the ‘Double’ class, not the ‘double’ type.

8  To traverse all of the elements of a simple array, it would be customary to use a for loop where the loop index cycles through values for the array subscript.

9  An ArrayList is different. Its elements can be traversed using a “for each” loop. Here is a simple example illustrating the “for each” syntax. Note that in reality, the word “each” does not appear in it. It is given this name to distinguish it from a simple for loop. ◦ For(Cup7 someCup : myCollection) { System.out.println(someCup); }

10  In the for statement a local reference named “someCup” is declared.  The for statement also contains a colon followed by the name of the ArrayList to be accessed, in this example named myCollection.  The type of the local reference agrees with the type of the elements of the ArrayList. The loop runs through all of the elements of myCollection, successively assigning references of its elements to the local reference, someCup.  Notice that the loop delivers access to successive elements of the collection, but it is not possible to call methods on the collection itself.  This means that it is not possible to add elements to, or remove them from the collection when using this syntax.

11  Traversing the elements of a collection like an ArrayList is known as iteration.  The Java API includes interfaces named Iterator and Iterable. These are specifications that support working with the elements of an ArrayList while traversing it.  The Iterable interface contains one method, named iterator(). Calling this method on an ArrayList returns a reference to an iterator.  When obtaining the iterator, in order for your code to be consistent, you want to use angle bracket notation to specify what kinds of elements the iterator is dealing with.  If myCollection contains Cup7 elements, then declaring and obtaining an iterator for it would take this form: ◦ Iterator myIterator = myCollection.iterator();

12  Three different methods can be called on an iterator: ◦ boolean hasNext()  Returns true if the iteration has more elements. ◦ E next()  Returns the next element in the iteration. ◦ void remove()  Removes from the underlying collection the last element returned by the iterator (optional operation).  These three methods make it possible to iterate through an ArrayList checking to see whether there is a “next” element, getting that element if there is one, and removing it if desired.

13  Here is an example using a while loop based on myCollection containing references to the Cup7 class. ◦ Iterator myIterator = myCollection.iterator(); Cup7 someCup; while(myIterator.hasNext()) { someCup = myIterator.next(); if (someCup.getSeedCount() <= 0) myIterator.remove(); }

14  The iterator for the collection is obtained before the loop, and the test for continuing to loop is a call to hasNext().  In the body of the loop, the call to next() returns a reference to the next element, and the call to remove() causes that element to be removed from the collection.  The example removes from myCollection all elements, all cups, which contain 0 or fewer seeds.

15  The following example accomplishes the same thing more compactly using a for loop. ◦ for(Iterator myIterator = myCollection.iterator(); myIterator.hasNext();) { if((myIterator.next().getSeedCount()) <= 0) myIterator.remove(); }

16  The first part of the for statement obtains the iterator for the collection.  The second part of the for statement causes the iteration to continue as long as there is another element in the collection.  Notice that the for loop is structured to contain three different things separated by semicolons, but it only contains two things and there is a concluding semicolon before the closing parenthesis.  The body of this loop is more compact than the while loop example. It removes an element without making use of a temporary reference.


Download ppt "ArrayLists.  The Java API includes a class named ArrayList. The introduction to the ArrayList class in the Java API documentation is shown below.  java.lang.Object."

Similar presentations


Ads by Google