List
is a collection construct, that is by definition
an
ordered collection, also known as a sequence. Unlike
the array, the number of the elements in the List can be reduced or
enlarged.List
collection can only hold objects. List
is an interface,
so it can't be
instantiated directly. There are two general-purpose List
implementations — ArrayList and LinkedList. The most
commonly used implementation, is ArrayList
.
It offers constant-time positional access, and it can take advantage to
move multiple elements at the same time.import
java.util.*; public class ListDemo { public static void main(String args[]) { // Create an array list ArrayList <String> al = new ArrayList <String>(); System.out.println("initial size:"+al.size()); // add elements to the array list al.add("C"); al.add("A"); al.add("E"); al.add("B"); al.add("D"); al.add("F"); System.out.println("the new size:"+al.size()); for(int i = 0; i <al.size(); i++){ System.out.print(al.get(i)+" "); } System.out.println(); String ex = al.remove (2); System.out.println("removing value:"+ex); System.out.println("size after remove:"+al.size()); for(String s : al){ System.out.print(s+ " "); } System.out.println(); // copy all elements in new ArrayList // 1.Using copy constructor ArrayList <String> sec = new ArrayList <String>(al); System.out.println("\tsec size: "+sec.size()); for(String s : sec){ System.out.print(s+ " "); } System.out.println(); // 2. Using method addAll() sec.clear(); System.out.println("\tsec size after clear(): "+sec.size()); sec.addAll(al); System.out.println("\t\tsec size after addAll(): "+sec.size()); for(String s : sec){ System.out.print(s+ " "); } System.out.println(); // Add just a portion of the second ArrayList sec.clear(); sec.addAll(al.subList(1, 4)); System.out.println("\t\tsec size after add subList(1,4): "+sec.size()); for(String s : sec){ System.out.print(s+ " "); } System.out.println(); } } |
initial size:0 the new size:6 C A E B D F removing value:E size after remove:5 C A B D F sec size: 5 C A B D F sec size after clear(): 0 sec size after addAll(): 5 C A B D F sec size after add subList(1,4): 3 A B D |
import
java.util.*; public class ListDemo1 { public static void main(String args[]) { // Create an array list ArrayList <Integer> al = new ArrayList <Integer>(); System.out.println("initial size:"+al.size()); // add elements to the array list al.add(10); al.add(20); al.add(30); al.add(40); al.add(50); al.add(60); System.out.println("the new size:"+al.size()); for(int i = 0; i <al.size(); i++){ System.out.print(al.get(i)+" "); } System.out.println(); Integer ex = al.remove (2); System.out.println("removing value:"+ex); System.out.println("size after remove:"+al.size()); for(Integer s : al){ System.out.print(s+ " "); } System.out.println(); // copy all elements in new ArrayList // 1.Using copy constructor ArrayList <Integer> sec = new ArrayList <Integer>(al); System.out.println("\tsec size: "+sec.size()); for(Integer s : sec){ System.out.print(s+ " "); } System.out.println(); // 2. Using method addAll() sec.clear(); System.out.println("\tsec size after clear(): "+sec.size()); sec.addAll(al); System.out.println("\t\tsec size after addAll(): "+sec.size()); for(Integer s : sec){ System.out.print(s+ " "); } System.out.println(); // Add just a portion of the second ArrayList sec.clear(); sec.addAll(al.subList(1, 4)); System.out.println("\t\tsec size after add subList(1,4): "+sec.size()); for(Integer s : sec){ System.out.print(s+ " "); } System.out.println(); } } |
initial size:0 the new size:6 10 20 30 40 50 60 removing value:30 size after remove:5 10 20 40 50 60 sec size: 5 10 20 40 50 60 sec size after clear(): 0 sec size after addAll(): 5 10 20 40 50 60 sec size after add subList(1,4): 3 20 40 50 |
Iterator
returned by List
's iterator()
operation returns the
elements of the list in proper sequence. import
java.util.*; public class IteratorDem { public static void main(String arg[]){ // Create an array list ArrayList <String> al = new ArrayList <String>(); // add elements to the array list al.add("C"); al.add("A"); System.out.println("ArrayList:"+al.get(0)+" "+al.get(1)); ListIterator<String> litr = al.listIterator(); // litr.set("new"); error- there is no element in the iterator System.out.println("before element "+litr.nextIndex()); System.out.println("after element "+litr.previousIndex()); System.out.println(litr.next()); System.out.println("before element "+litr.nextIndex()); System.out.println("after element "+litr.previousIndex()); litr.set("modified"); // modify the last element returned // by next() or previous() System.out.println("ArrayList:"+al.get(0)+" "+al.get(1)); } } |
ArrayList:C A before element 0 after element -1 C before element 1 after element 0 ArrayList:modified A |
import
java.util.*; public class IteratorDemo { public static void main(String args[]) { // Create an array list ArrayList <String> al = new ArrayList <String>(); // add elements to the array list al.add("C"); al.add("A"); al.add("E"); al.add("B"); al.add("D"); al.add("F"); // Use iterator to display contents of al System.out.print("Original contents of al: "); Iterator<String> itr = al.iterator(); while(itr.hasNext()) { System.out.print(itr.next() + " "); } System.out.println(); // Modify objects being iterated ListIterator<String> litr = al.listIterator(); while(litr.hasNext()) { litr.set(litr.next() + "+"); } System.out.print("Modified contents of al: "); itr = al.iterator(); while(itr.hasNext()) { System.out.print(itr.next() + " "); } System.out.println(); // Now, display the list backwards System.out.print("Modified list backwards: "); while(litr.hasPrevious()) { System.out.print(litr.previous() + " "); } System.out.println(); } } |
Original contents of al: C A E B D F Modified contents of al: C+ A+ E+ B+ D+ F+ Modified list backwards: F+ D+ B+ E+ A+ C+ |