- List interface inwards coffee is ordered collection then it tin shop elements inwards sequence.
- List interface is sub interface too it extends to Collection interface.
- List interface tin concur duplicate elements then you lot tin insert duplicate elements too.
- You tin access elements from listing past times integer index.
- Collection framework has many interfaces inwards coffee too listing interface is ane of them.
- List interface provides ListIterator to traverse through elements listing inwards backward too frontward directions.
Few by too large used methods inwards listing interface are equally bellow.
- void add(int index,Object element) : It volition insert chemical cistron at specified index seat inwards list.
- boolean addAll(int index,Collection c) : It volition add together all elements of given collection inwards to the listing at specified index position.
- object get(int Index ) : It volition render object at specified index seat from list.
- object set(int index,Object element) : It volition assign object at specified index seat inwards list.
- object remove(int index) : It volition take away object from specified position.
- ListIterator listIterator() : It volition returns the listing iterator at start of invoking list.
- ListIterator listIterator(int x) : It volition returns the listing iterator at specified index seat inwards list.
- int indexOf(Object obj) : It volition render index of rootage illustration of object from invoking list.
- int lastIndexOf(Object obj) : It volition render index of concluding illustration of object from invoking list.
List interface is implemented past times many classes similar Arraylist, Linkedlist too Vector. Here, I accept represented illustration of listing interface using few methods from dissimilar degree implementation.
package JAVAExamples; import java.util.ArrayList; import java.util.LinkedList; import java.util.List; import java.util.ListIterator; populace degree InterfaceList { populace static void main(String[] args) { // Add elements inwards ArrayList. List al = novel ArrayList(); al.add("New York"); al.add("Delhi"); al.add("Tokyo"); // Print arraylist elements. System.out.println("ArrayList Elements"); System.out.print(al); System.out.println(); // Add elements inwards linkedlist. List ll = novel LinkedList(); ll.add("New York"); ll.add("Delhi"); ll.add("Tokyo"); // Print linkedlist elements. System.out.println(); System.out.println("LinkedList Elements"); System.out.print(ll); System.out.println(); // Get too impress arraylist's third element. System.out.println(); System.out.println("Element at third seat inwards arralist is : " + al.get(2)); // Using ListIterator to traverse through frontward too backward directions inwards arralist. ListIterator<String> itrtr = al.listIterator(); // Traverse inwards frontward direction. System.out.println(); System.out.println("Traversing through arralist elements inwards frontward direction..."); piece (itrtr.hasNext()) { System.out.println(itrtr.next()); } // Traverse inwards backward direction. System.out.println(); System.out.println("Traversing through arralist elements inwards backward direction..."); piece (itrtr.hasPrevious()) { System.out.println(itrtr.previous()); } } }
Output
ArrayList Elements [New York, Delhi, Tokyo] LinkedList Elements [New York, Delhi, Tokyo] Element at third seat inwards arralist is : Tokyo Traversing through arralist elements inwards frontward direction... New York Delhi Tokyo Traversing through arralist elements inwards backward direction... Tokyo Delhi New York
In higher upwardly illustration you lot tin encounter how arralist, linkedlist, croak method too listiterator works.