Monday, January 12, 2015

Java Using Linked List Program



How to copy vector to array

package com.java2novice.vector;
import java.util.Vector;
public class MyVectorArrayCopy
{
public static void main(String a[])
{
        Vector vct = new Vector();
        vct.add("First");
        vct.add("Second");
        vct.add("Third");
        vct.add("Random");
        System.out.println("Actual vector:"+vct);
        String[] copyArr = new String[vct.size()];
        vct.copyInto(copyArr);
        System.out.println("Copied Array content:");
        for(String str:copyArr)
       {
            System.out.println(str);
        }
    }
}

How to get sub list from vector

package com.java2novice.vector;
import java.util.List;
import java.util.Vector;
public class MyVectorSubRange
{
public static void main(String a[])
{
        Vector vct = new Vector();
        vct.add("First");
        vct.add("Second");
        vct.add("Third");
        vct.add("Random");
        vct.add("Click");
        System.out.println("Actual vector:"+vct);
        List list = vct.subList(2, 4);
        System.out.println("Sub List: "+list);
    }
}

Basic Linked List Operations

package com.java2novice.linkedlist;
import java.util.LinkedList;
public class MyBasicOperations
{
public static void main(String a[])
{
        LinkedList ll = new LinkedList();
        ll.add("Orange");
        ll.add("Apple");
        ll.add("Grape");
        ll.add("Banana");
        System.out.println(ll);
        System.out.println("Size of the linked list: "+ll.size());
        System.out.println("Is LinkedList empty? "+ll.isEmpty());
        System.out.println("Does LinkedList contains 'Grape'? "+ll.contains("Grape"));
    }
}

How to read all elements in Linked List by using iterator

package com.java2novice.linkedlist;
import java.util.Iterator;
import java.util.LinkedList;
public class MyLinkedListIterate
{
 public static void main(String a[])
{
        LinkedList arrl = new LinkedList();
        arrl.add("First");
        arrl.add("Second");
        arrl.add("Third");
        arrl.add("Random");
        Iterator itr = arrl.iterator();
        while(itr.hasNext())
         {
            System.out.println(itr.next());
        }
    }
}

How to add all elements of a list to Linked List

package com.java2novice.linkedlist;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;
public class MyLinkedListNewCollection
{
public static void main(String a[])
{
        LinkedList arrl = new LinkedList();
        arrl.add("First");
        arrl.add("Second");
        arrl.add("Third");
        arrl.add("Random");
        System.out.println("Actual LinkedList:"+arrl);
        List list = new ArrayList();
        list.add("one");
        list.add("two");
        arrl.addAll(list);
        System.out.println("After Copy: "+arrl);
    }
}

No comments:

Post a Comment