Monday, May 26, 2014

Java: Arraylist

A bit introduction:
  Arraylist is a class in java.
  A constructor must be made for adding the data

Initialization:
   ArrayList list = new ArrayList();
Adding data:
   list.add(new Student(matric, name, totalpoint, totalCH, gpa, cgpa));

Retrieving data:
   double cgpa= student.getCGPA();

Update data:
   student.setTotalPoint(tp);

To go through the list / iterating through the list :
   for (senior student : list2) 
   { //to do code here
   }

Getting list size/length:
   int s= list2.size();

Getting specific element of list:
   for(int i =0; i
   {   list.get(i).getcgpa()
   }

Swapping element of list:
   list.set(i, list.get(i+1));    // listname.set(list index, new element)

Printing list:
   for(senior l: list)
      System.out.println(l.toString()); // where t.String method is the method in Student constructor