/* * LinkedList.java * * Created on January 10, 2008, 8:51 PM * * To change this template, choose Tools | Template Manager * and open the template in the editor. */ package linkedlist; import java.util.List; import java.util.LinkedList; import java.util.Iterator; import java.util.ListIterator; import java.util.Collections; import java.util.Random; /** * * @author Sayed */ public class LinkedListTest { /** Creates a new instance of LinkedList */ public LinkedListTest() { } /** *Example operations using linked lists */ public void linkedListOperation(){ final int MAX = 10; int counter = 0; //create two linked lists List listA = new LinkedList(); List listB = new LinkedList(); //store data in the linked list A for (int i = 0; i < MAX; i++) { System.out.println(" - Storing Integer(" + i + ")"); listA.add(new Integer(i)); } //print data from the linked list using iterator Iterator it = listA.iterator(); while (it.hasNext()) { System.out.println(it.next()); } //print data from the linked list using listIterator. counter = 0; ListIterator liIt = listA.listIterator(); while (liIt.hasNext()) { System.out.println("Element [" + counter + "] = " + liIt.next()); System.out.println(" - hasPrevious = " + liIt.hasPrevious()); System.out.println(" - hasNext = " + liIt.hasNext()); System.out.println(" - previousIndex = " + liIt.previousIndex()); System.out.println(" - nextIndex = " + liIt.nextIndex()); System.out.println(); counter++; } //retrieve data from the linked list using index for (int j=0; j < listA.size(); j++) { System.out.println("[" + j + "] - " + listA.get(j)); } //find the location of an element int locationIndex = listA.indexOf("5"); System.out.println("Index location of the String \"5\" is: " + locationIndex); //find the first and the last location of an element System.out.println("First occurance search for String \"5\". Index = " + listA.indexOf("5")); System.out.println("Last Index search for String \"5\". Index = " + listA.lastIndexOf("5")); //create a sublist from the list List listSub = listA.subList(10, listA.size()); System.out.println("New Sub-List from index 10 to " + listA.size() + ": " + listSub); //sort the sub-list System.out.println("Original List : " + listSub); Collections.sort(listSub); System.out.println("New Sorted List : " + listSub); System.out.println(); //reverse the new sub-list System.out.println("Original List : " + listSub); Collections.reverse(listSub); System.out.println("New Reversed List : " + listSub); System.out.println(); //check to see if the lists are empty System.out.println("Is List A empty? " + listA.isEmpty()); System.out.println("Is List B empty? " + listB.isEmpty()); System.out.println("Is Sub-List empty? " + listSub.isEmpty()); //compare two lists System.out.println("A=B? " + listA.equals(listB)); System.out.println(); //Shuffle the elements around in some Random order for List A Collections.shuffle(listA, new Random()); //convert a list into an array Object[] objArray = listA.toArray(); for (int j=0; j < objArray.length; j++) { System.out.println("Array Element [" + j + "] = " + objArray[j]); } //clear listA System.out.println("List A (before) : " + listA); System.out.println(); listA.clear(); System.out.println("List A (after) : " + listA); System.out.println(); } /** * @param args the command line arguments */ public static void main(String[] args) { // TODO code application logic here LinkedListTest listExample = new LinkedListTest(); listExample.linkedListOperation(); } }
J2SE : LinkedList and Iterators in Java
Permanent link to this article: http://bangla.sitestree.com/j2se-linkedlist-and-iterators-in-java/