Wednesday, 27 March 2013

JAVA PROGRAMS USING LINKEDLIST, GENERIC COLLECTIONS FOR STUDENT LIST


 import java.util.*; /* scanner function is used*/

class StudList {
  private String name;
  private String USN;
  private String college;

   StudList(String n, String s, String c) {
    name = n;
    USN = s;
    college = c;
  }

  public String toString() {
    return name + ":" + USN + ":" + college;
  }
}

public class FourB {
  public static void main(String args[]) 
{
  LinkedList<StudList> ml = new LinkedList<StudList>();

  ml.add(new StudList("Shwetha", "1MS00IS101", "MSRIT"));
  ml.add(new StudList("Mishti", "1MS00IS102", "RVCE"));
  ml.add(new StudList("Purvi ", "1MS00IS103","BMSCE"));
  ml.add(new StudList("Aditya", "1MS00IS104", "PESIT"));
  ml.add(new StudList("Rohit", "1MS00IS105", "NMIT"));


System.out.println("Accessing Via Iterator....");
Iterator<StudList> it = ml.iterator();

while (it.hasNext()) {
   String element=it.next().toString();
   System.out.println(element + "");
}

System.out.println("Number of students in the list: "+ml.size());

System.out.println("Which student do you want to remove?");
Scanner s = new Scanner(System.in);
int pos = s.nextInt();
ml.remove(pos);

System.out.println("New Kind of For Loop...List is now...");
    for (StudList element : ml){
      System.out.println(element);
    }
System.out.println("Number of students in the list is"+ml.size());
System.out.println("Shuffling the list...");
Collections.shuffle(ml);
System.out.println("List after shuffling is ...");
    for (StudList element : ml){
      System.out.println(element);
    }

System.out.println("First Student is :"+ml.getFirst());
System.out.println("Last Student is :"+ml.getLast());
  }
}



No comments:

Post a Comment