Thursday, October 30, 2014

Java Linked Pro



Search elements of Linked List Java

import java.util.LinkedList; 
public class SearchElementLinkedListExample
{
public static void main(String[] args)
{
LinkedList lList = new LinkedList();
lList.add("1");
lList.add("2");
lList.add("3");
lList.add("4");
lList.add("5");
lList.add("2");
int index = lList.indexOf("2");
if(index != -1)
{
System.out.println("First occurrence of 2 in LinkedList is at index : "
+ index);
}
else
{
System.out.println("LinkedList does not contain 2");
}
index = lList.lastIndexOf("2");
if(index != -1)
{
System.out.println("Last occurrence of 2 in LinkedList is at index : "
+ index);
}
else
{
System.out.println("LinkedList does not contain 2");
     }
  }
}

Simple Java Linked List

import java.util.LinkedList;
public class SimpleJavaLinkedListExample
{
public static void main(String[] args)
{
LinkedList lList = new LinkedList();
lList.add("1");
lList.add("2");
lList.add("3");
lList.add("4");
lList.add("5");
System.out.println("LinkedList contains : " + lList);
   }
}

Add or insert an element to Array List using Java List Iterator

import java.util.ListIterator;
import java.util.ArrayList;
public class AddAnElementUsingListIteratorExample
{
public static void main(String[] args)
{
ArrayList aList = new ArrayList();
aList.add("1");
aList.add("2");
aList.add("3");
aList.add("4");
aList.add("5");
ListIterator listIterator = aList.listIterator(); listIterator.next(); listIterator.add("Added Element");
System.out.println("After inserting element, ArrayList contains");
for(int intIndex = 0; intIndex < aList.size(); intIndex++)
System.out.println(aList.get(intIndex));
   }
}

Get Previous and next index using Java List Iterator

import java.util.ListIterator;
import java.util.ArrayList;  
public class GetPreviousAndNextIndexListIteratorExample
{
public static void main(String[] args)
{
ArrayList aList = new ArrayList();
aList.add("1");
aList.add("2");
aList.add("3");
aList.add("4");
aList.add("5");
ListIterator listIterator = aList.listIterator();
System.out.println("Previous Index is : " + listIterator.previousIndex());  
System.out.println("Next Index is : " + listIterator.nextIndex());
listIterator.next();
System.out.println("After increasing current position by one element : ");
System.out.println("Previous Index is : " + listIterator.previousIndex());  
System.out.println("Next Index is : " + listIterator.nextIndex());
    }
}

Remove an element from Array List using Java List Iterator

import java.util.ListIterator;
import java.util.ArrayList;
public class RemoveAnElementUsingListIteratorExample
{
public static void main(String[] args)
{
ArrayList aList = new ArrayList();
aList.add("1");
aList.add("2");
aList.add("3");
aList.add("4");
aList.add("5");
ListIterator listIterator = aList.listIterator();
listIterator.next();
listIterator.next();
listIterator.remove();
System.out.println("After removing 2, ArrayList contains");
for(int intIndex = 0; intIndex < aList.size(); intIndex++)
System.out.println(aList.get(intIndex));
    }
}

No comments:

Post a Comment