0

I successfully sort students by id using comparator and displayed using for loop, I hope to know how can I make it happen with iterator, well I tried but only address in console and have no idea of how to print info from Student class

public static void main(String[] args) {
    Student stu1 = new Student("Lucy",1000,99);
    Student stu2 = new Student("Jack",1001,22);
    Student stu3 = new Student("Peter",1002,53);

    ArrayList<Student> student = new ArrayList<Student>();
    student.add(stu1);
    student.add(stu2);
    student.add(stu3);

    Collections.sort(student,new StuCOmp("id"));
    for (int i=0; i<student.size(); i++) {
        Student stu = student.get(i);
        System.out.println(stu.getName() + " "+stu.getId()+ " "+stu.getGrade());    
    }

    Iterator<Student> it = student.iterator();
    while(it.hasNext()){

        //what do I write here
    }
}
ashatte
  • 5,442
  • 8
  • 39
  • 50
ZoeH
  • 109
  • 9
  • 2
    Iterator doesn't have many methods. Have you checked uits javadoc? http://docs.oracle.com/javase/7/docs/api/java/util/Iterator.html – JB Nizet Jul 08 '14 at 10:06
  • You basically do the same thing as you would do with a normal `for` loop. Assign `it.next()` to temp variable, do whatever, repeat. – awksp Jul 08 '14 at 10:06
  • duplicate of http://stackoverflow.com/questions/5228687/java-best-way-to-iterate-through-an-collection-here-arraylist – AJJ Jul 08 '14 at 10:09

6 Answers6

1

You can iterate it like this in for loop -

       for (Iterator<Student> iterator = student.iterator(); iterator.hasNext();) {
            Student stu = iterator.next();
            System.out.println(stu.getName() + " "+stu.getId()+ " "+stu.getGrade());
        }
Ninad Pingale
  • 6,801
  • 5
  • 32
  • 55
1
Iterator itr=student.iterator();  
  while(itr.hasNext()){  
    Student stu=(Student)itr.next();  
    System.out.println(stu.getName() + " "+stu.getId()+ " "+stu.getGrade());  
saurabh kumar
  • 155
  • 5
  • 26
  • the sys out prints the object means as hash code of object student will be printed. No use of it. – AJJ Jul 08 '14 at 10:10
1
Iterator<Student> it = student.iterator();
    while(it.hasNext()){
        // You should write here:   
        Student stu = it.next();
        System.out.println(stu.getName() + " "+stu.getId()+ " "+stu.getGrade());
     }
dips
  • 76
  • 4
1

You can avoid using an iterator and use an enhanced for loop.

for (Student st : student)
    System.out.println(st.getName() + " "+st.getId()+ " "+st.getGrade());
Stelium
  • 1,207
  • 1
  • 12
  • 23
0

You can do something like this

Iterator<Student> it = student.iterator();
while(it.hasNext()){
  Student student=it.next();// you can get the students one by one  
  // Now you can get the each student info
  //student.any attribute
}

This way is very similar to for-each but iterator is faster than that.

Ruchira Gayan Ranaweera
  • 34,993
  • 17
  • 75
  • 115
0
Iterator itr=student.iterator();  
  while(itr.hasNext()){  
    Student stu=(Student)itr.next();  
    System.out.println("whatever you want to print");  
sdvadsa
  • 77
  • 1
  • 4
  • 12