Monday, January 12, 2015

Linked List to Array Program in Java



How to delete all elements from my Linked List

package com.java2novice.linkedlist;
import java.util.LinkedList;
public class ClearMyLinkedList
{
 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);
        arrl.clear();
        System.out.println("After clear LinkedList:"+arrl);
    }
}

How to find does Linked List contains all list elements or not

package com.java2novice.linkedlist;
import java.util.LinkedList;
import java.util.List;
public class MyElementCheck
{
public static void main(String a[])
{
        LinkedList arrl = new LinkedList();
        arrl.add("First");
        arrl.add("Second");
        arrl.add("Third");
        arrl.add("Random");
        List list = new LinkedList();
        list.add("Second");
        list.add("Random");
        System.out.println("Does LinkedList contains all list elements?: "
         +arrl.containsAll(list));
        list.add("one");
        System.out.println("Does LinkedList contains all list elements?: "
         +arrl.containsAll(list));
    }
}

How to copy Linked List to array

package com.java2novice.linkedlist;
import java.util.LinkedList;
public class MyLinkedListArray
{
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);
        String[] strArr = new String[arrl.size()];
        arrl.toArray(strArr);
        System.out.println("Created Array content:");
        for(String str:strArr)
        {
            System.out.println(str);
        }
    }
}

How to get sub list from Linked List

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

How to sort Linked List using Comparator

package com.java2novice.linkedlist;
import java.util.Collections;
import java.util.Comparator;
import java.util.LinkedList;
public class MyLinkedListSort
{
 public static void main(String a[])
{
        LinkedList list = new LinkedList();
        list.add(new Empl("Ram",3000));
        list.add(new Empl("John",6000));
        list.add(new Empl("Crish",2000));
        list.add(new Empl("Tom",2400));
        Collections.sort(list,new MySalaryComp());
        System.out.println("Sorted list entries: ");
        for(Empl e:list)
        {
            System.out.println(e);
        }
    }
}
class MySalaryComp implements Comparator
{
public int compare(Empl e1, Empl e2)
{
        if(e1.getSalary() < e2.getSalary())
        {
            return 1;
        }
        else
       {
            return -1;
        }
    }
}
 class Empl
{
     
    private String name;
    private int salary;
public Empl(String n, int s)
          {
        this.name = n;
        this.salary = s;
 }   
public String getName()
   {
        return name;
    }
    public void setName(String name)
    {
        this.name = name;
    }
    public int getSalary()
   {
        return salary;
    }
    public void setSalary(int salary)
    {
        this.salary = salary;
    }
    public String toString()
    {
        return "Name: "+this.name+"-- Salary: "+this.salary;
    }
}

No comments:

Post a Comment