迭代器类

问题描述:

的数组列表我是Java的新手,所以如果我的问题看起来很愚蠢,请耐心等待。迭代器类

我正在学习集合,我已经编写了一个程序来存储学生姓名和标记。我将所有这些存储在Arraylist中。 使用for循环我能够在我的程序中打印值。 但我无法知道如何用Iterator做到这一点。我的代码是这样的。

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这是一个主类,我

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()); 

    } 
    }  
} 

在这里,在这种情况下,我得到正确的输出。

如果我删除了循环和更换由iterator.I我得到了一些错误,是说Object can not be converted as Student

这里是我的代码

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()); 
} 

请指引我这里有什么问题以及如何纠正。

当你拥有它,你必须明确地投它:

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

或更改您的ListIterator使用泛型

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

但另一种更容易阅读的替代将是一个foreach循环

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

@Downvoter - care to comment? –

+0

谢谢,这正是我要找的,类型铸造:) – Jayababu

+0

我只是downvoted所有的答案,只有建议铸造,这绝对不是什么OP是寻找(尽管他声称这就是他正在寻找)。 – Kayaman

您错过了ListIterator的仿制药声明。

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

没有理由,开始在这个时代铸造。

问题在于如何创建ListIterator实例以获取迭代器进行导航。

您的以下语句将假定ListIterator用于默认引用类型,即Object。

ListIterator itr=details.listIterator(); 

通过指定您正在处理的参考对象的确切类型,在您的案例'学生'中更改上述语句。

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

这应该可以解决您的问题。让我知道如果它。

有两种方法可以解决这个问题。使用泛型或添加明确的转换。您可以在ListIterator中使用泛型。

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

或者用另一种方式,您可以将从Iterator接收的对象转换为Student。

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

请提及您将使用的参考对象的类型。 在你的情况下,它将是学生对象。

/* 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()); 
} 

如果要使用Iterator pattern。您的代码应该是:

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()); 
} 

你应该看一看的Javadoc ListIterator以了解IteratorListIterator之间的不同。

随着ListIterator可以

  • 迭代向后。
  • 在任何时候获得索引。
  • 在任何点添加一个新值。
  • 在这一点上设置了一个新值。