java.util.List
Lists are collections where the elements are ordered.Elements in the list has a position in the list.The index of the elements in the list starts from 0.List supports all of the collections operation and in addition to it , it also support position based access, search and open-view range operations.
Some of the methods available in the List are
get(int index)- To access an element.
set(int index,E element) - To set an element in the List in nth position.
add(int index,E element) - Add element in the list
addAll(int index,Collections <? extends E> c)- Add a collection to the list
E remove(int index) - Remove elements from nth position.
Methods for elements search are
int indexOf(Object o)
int lastIndexOf(Object o)
Open view range method
List <E> subList(int indexFrom,int toIndex)
Iterators :
ListIterators()
ListIterators<E> listIterator()
ListIterators<E> listIterator(index i) - Traverse from the element indicated by the index i.
ListIterator is the biderectional iterator for List
ArrayList<E>,LinkedList<E>,Vector<E> classes
ArrayList and Vector class implements List interface. Both are implemented using dynamically resizable arrays provide fast random access .Vector is threadsafe and it has slight performance penalty due to synchronization.LinkedList implementation uses double linked list.If are lot of insertion and removal of elements LinkedList is the best choice otherwise most of the cases ArrayList is the preferred choice.
ArrayLsitConstructors
ArrayList()
ArrayList(Collection <? extends E > c) - Add elements in the collection to the ArrayList
ArrayList (int initialCapacity) - Specifies the initial capacity of the array list.
package collections;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Vector;
public class ListEg {
public static void main(String[] args) {
List<String> aL = new ArrayList<String>();
aL.add("Test");
System.out.println(aL.get(0));
List<String> alColl = new ArrayList<String>(aL);
alColl.add("Test1");
System.out.println(alColl);
aL = new LinkedList<String>();
aL.add("Test");
System.out.println(aL.get(0));
alColl = new LinkedList<String>(aL);
alColl.add("Test1");
System.out.println(alColl);
alColl.remove(1);
System.out.println(alColl);
aL = new Vector<String>();
aL.add("Test");
System.out.println(aL.get(0));
alColl = new Vector<String>(aL);
alColl.add("Test1");
System.out.println(alColl);
ListIterator<String> litr = alColl.listIterator();
while (litr.hasNext()) {
System.out.println(litr.next());
}
litr = alColl.listIterator(1);
while (litr.hasNext()) {
System.out.println("indexIterator=" + litr.next());
}
}
}
Lists are collections where the elements are ordered.Elements in the list has a position in the list.The index of the elements in the list starts from 0.List supports all of the collections operation and in addition to it , it also support position based access, search and open-view range operations.
Some of the methods available in the List are
get(int index)- To access an element.
set(int index,E element) - To set an element in the List in nth position.
add(int index,E element) - Add element in the list
addAll(int index,Collections <? extends E> c)- Add a collection to the list
E remove(int index) - Remove elements from nth position.
Methods for elements search are
int indexOf(Object o)
int lastIndexOf(Object o)
Open view range method
List <E> subList(int indexFrom,int toIndex)
Iterators :
ListIterators()
ListIterators<E> listIterator()
ListIterators<E> listIterator(index i) - Traverse from the element indicated by the index i.
ListIterator is the biderectional iterator for List
ArrayList<E>,LinkedList<E>,Vector<E> classes
ArrayList and Vector class implements List interface. Both are implemented using dynamically resizable arrays provide fast random access .Vector is threadsafe and it has slight performance penalty due to synchronization.LinkedList implementation uses double linked list.If are lot of insertion and removal of elements LinkedList is the best choice otherwise most of the cases ArrayList is the preferred choice.
ArrayLsitConstructors
ArrayList()
ArrayList(Collection <? extends E > c) - Add elements in the collection to the ArrayList
ArrayList (int initialCapacity) - Specifies the initial capacity of the array list.
package collections;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;
import java.util.Vector;
public class ListEg {
public static void main(String[] args) {
List<String> aL = new ArrayList<String>();
aL.add("Test");
System.out.println(aL.get(0));
List<String> alColl = new ArrayList<String>(aL);
alColl.add("Test1");
System.out.println(alColl);
aL = new LinkedList<String>();
aL.add("Test");
System.out.println(aL.get(0));
alColl = new LinkedList<String>(aL);
alColl.add("Test1");
System.out.println(alColl);
alColl.remove(1);
System.out.println(alColl);
aL = new Vector<String>();
aL.add("Test");
System.out.println(aL.get(0));
alColl = new Vector<String>(aL);
alColl.add("Test1");
System.out.println(alColl);
ListIterator<String> litr = alColl.listIterator();
while (litr.hasNext()) {
System.out.println(litr.next());
}
litr = alColl.listIterator(1);
while (litr.hasNext()) {
System.out.println("indexIterator=" + litr.next());
}
}
}
No comments:
Post a Comment