Search code examples
javacollectionsiteratorlistiterator

Iterator for an array list of Class


Am New to Java so please bear with me if my question seems to be silly.

I am learning collections,I have written a program which will store student names id and marks.I am storing all these in a Arraylist. Using for loop I am able to print the values in my program. But I am not able to get an idea how to do that with an Iterator. My code goes like this.

Student.Java

public class Student{
   private String name;
   private String rollno;
   private String biology;
   private String maths;
   private String science;

   public Student(String name,String rollno,String biology,String maths,String science){
      setName(name);
      setRollno(rollno);
      setBiology(biology);
      setMaths(maths);
      setScience(science);
   }       

   public void setRollno(String rollno){
      this.rollno=rollno;
   }

   public String getRollno(){
      return rollno;
   }

   public void setName(String name){
      this.name=name;
   }

   public String getName(){
      return name;
   }

   public void setBiology(String biology){
      this.biology=biology;
   }

   public String getBiology(){
      return biology;
   }

   public void setMaths(String maths){
      this.maths=maths;
   }

   public String getMaths(){
      return maths;
   }

   public void setScience(String science){
      this.science=science;
   }

   public String getScience(){
      return science;
   }


}

College.Java which is a main class for me

import java.util.*;

public class College{

public static void main(String args[]){

    ArrayList<Student> details = new ArrayList<Student>();
    Student Jb=new Student("Jb   ","417","92","97","90");
    Student Laxman=new Student("Lucky","418","93","97","96");
    Student Ajay=new Student("AJ   ","419","89","95","93");
    Student Venky=new Student("Venky","420","96","90","91");
    Student Satti=new Student("Satti","421","70","94","96");

    details.add(Jb);

    details.add(Laxman);
    details.add(Ajay);
    details.add(Venky);
    details.add(Satti);

    for(int i=0;i<details.size();i++)
    {
        Student student=details.get(i);

        System.out.println("Student Name : "+student.getName()+" Roll Number : "+student.getRollno()+" Biology : "+student.getBiology()+" Maths : "+student.getMaths()+" Science : "+student.getScience());

    }
  }    
}

Here in this case I am getting correct output.

If i remove for loop and replace by an iterator.I am getting some error which is saying Object can not be converted as Student

Here is my code

ListIterator itr=details.listIterator();
while(itr.hasNext())
{
   //Object student=itr.next();
   Student student=itr.next();
   System.out.println("Student Name : "+student.getName()+" Roll Number : "+student.getRollno()+" Biology : "+student.getBiology()+" Maths : "+student.getMaths()+" Science : "+student.getScience());
}

Please guide me what is wrong here and how to correct.


Solution

  • As you have it, you have to cast it explicitly:

     Student student=(Student)itr.next();
    

    or change your ListIterator to use generics

    ListIterator<Student> itr=details.listIterator();
    

    But another easier to read alternative would be a foreach loop

    for (Student student : details) {
         System.out.println("Student Name : "+student.getName()+" Roll Number : "+student.getRollno()+" Biology : "+student.getBiology()+" Maths : "+student.getMaths()+" Science : "+student.getScience());
    }