Iterator for an array list of Class

3.6k views Asked by At

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.

6

There are 6 answers

0
Scary Wombat On BEST ANSWER

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());
}
0
karan shah On

Please mention the type of reference object which you will be using. In your case it would be Student Object.

/* If you want to use iterator here is the sample code */
 ListIterator<Student> studentItr = details.listIterator();
 while(studentItr.hasNext())//This method returns boolean value,if there is an element and pointer has not reached at the end of list it returns true.
 {
    Student student = studentItr.next();//Method returns the next element and proceeds the cursor tp next index.
    System.out.println(student.getName());
    System.out.println(student.getRollno());
 }
0
codeaholicguy On

If you want to use Iterator pattern. Your code should be:

Iterator<Student> itr = details.iterator();
while (itr.hasNext()) {
     Student student = itr.next();
     System.out.println("Student Name : " + student.getName() + " Roll Number : " + student.getRollno() + " Biology : " + student.getBiology() + " Maths : " + student.getMaths() + " Science : " + student.getScience());
}

You should take a look at the Javadoc for ListIterator in order to understand the different between Iterator and ListIterator.

With ListIterator you can

  • iterate backwards.
  • obtain the index at any point.
  • add a new value at any point.
  • set a new value at that point.
0
Kayaman On

You're missing the generics declaration from your ListIterator.

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

There's no reason to start casting in this day and age.

0
Paul Arockiam On

The problem is with how you have created the ListIterator instance to get the iterator to navigate.

The below statement of yours would assume the ListIterator is for default reference type, which is Object.

ListIterator itr=details.listIterator();

Change the above statement by specifying what exact type of reference object you are dealing with, in your case 'Student'.

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

That should solve your problem. Let me know if it does.

0
Abhijeet Dhumal On

There are two ways to solve this. Using generics or adding a explicit casting. You can use generics with ListIterator.

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

Or with the other way you can cast object receiving from Iterator to Student.

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