

Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An overview of the java list interface and its implementing classes: linkedlist, arraylist, and vector. It explains the methods available in the list interface, such as add(), contains(), get(), and size(), and demonstrates how to create a list object using different methods. It also mentions the arrays.aslist() method and discusses some important features of these list types.
Typology: Study notes
1 / 3
This page cannot be seen from the preview
Don't miss anything!


Classes that implement the List interface : Java includes three classes ( LinkedList , ArrayList , and Vector ) that implement the List interface This interface and the three classes are made available by importing java.util.* :
List method signature Action void add(int index, Object o) Inserts the object o at the position specified by index after all existing objects at that index and greater are moved forward one position. boolean add(Object o) Appends o to the end of the list. Returns true. boolean addAll(Collection c) Appends c to the end of the list. void clear( ) Removes all elements from the list. boolean contains(Object o) Returns true if this list contains the specified object. boolean containsAll(Collection c) Returns true if this list contains all of the elements in c. boolean equals(Object o) Returns true if List object o has the same elements in the same order as the present list. If o is any other collection such as a Set , a false is returned. Object get(int index) Returns the object at the position specified by index. int indexOf(Object o) Returns the index of the first occurrence of the specified object…searching from left to right.…or –1 if not found. boolean isEmpty( ) Returns true if this list contains no objects. Iterator iterator( ) Returns an Iterator object for this list …Important…to be discussed in the next chapter. int lastIndexOf(Object o) Returns the index of the first occurrence of o … when searching from right to left .…or –1 if the object is not found. ListIterator listIterator( ) Returns a ListIterator object for this list … Important…to be discussed in the next chapter. Object remove(int index) Removes the object at the position specified by index and returns the object. boolean remove(Object o) Remove the first occurrence of o (searching from left to right). boolean removeAll(Collection c) Removes from this list the first occurrence of all elements in c. boolean retainAll(Collection c)) Retains only the elements in c. Object set(int index, Object o) Replaces the object at the position specified by index with o…Returns old object. int size( ) Returns the number of objects in the list Object[] toArray( ) Returns an Object array in the proper sequence.
Printing a List object: It is possible to print the contents of an entire list named lst with System.out.println(lst). A typical printout would look like the following if characters ‘a’ – ‘g’ are stored as the individual elements of the list (notice the surrounding square brackets):
[a, b, c, d, e, f, g]
Creating a List object: There are three ways to create a List object since there are three classes (mentioned above) that implement the List interface.
**1. List lst = new LinkedList( );
The Arrays.asList( ) method: If ary is an ordinary, singly-subscripted array, then Arrays.asList(ary) will return a List object in which the elements of the list are the elements of ary. It is also possible to make very simple lists easily as shown by the following:
List lst = Arrays.asList(“A”, “B”, “C”, “D”);
It should be noted that it is not possible to add or remove elements from the resulting List object; however, it is possible to use the set method to change values. Iterators can be produced from the list and used to step through the items in the list.
Important features: Here are some salient facts about these three List types:
On your own: This lesson has been purposely left vague and sparse. The exercises that follow are all “doable” using the information in this lesson, especially the descriptions of the methods in the interface on the preceding page. The student is on his own to “ferret out” the information needed to answer the questions. As an example of doing this, consider problem 3 on the next page. Even though Iterator objects have not been presented yet, just look over the methods in the interface and see which one deals with an Iterator. No knowledge of what an Iterator is or how it works is actually needed.