1

我是 Java 新手,所以如果我的问题看起来很愚蠢,请多多包涵。

我正在学习收藏,我编写了一个程序来存储学生姓名 id 和标记。我将所有这些存储在一个 Arraylist 中。使用 for 循环我可以在我的程序中打印值。但是我不知道如何使用迭代器来做到这一点。我的代码是这样的。

学生.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());

    }
  }    
}

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

如果我删除 for 循环并用迭代器替换。我收到一些错误,这就是说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());
}

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

4

6 回答 6

3

正如你所拥有的,你必须明确地转换它:

 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());
}
于 2015-06-16T06:59:59.093 回答
3

您缺少ListIterator.

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

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

于 2015-06-16T07:00:36.180 回答
3

有两种方法可以解决这个问题。使用泛型或添加显式转换。您可以将泛型与 ListIterator 一起使用。

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

或者通过其他方式,您可以将对象从迭代器接收到学生。

Student student=(Student) itr.next();
于 2015-06-16T07:03:20.213 回答
1

问题在于您如何创建 ListIterator 实例以使迭代器导航。

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

ListIterator itr=details.listIterator();

通过指定您正在处理的参考对象的确切类型来更改上述语句,在您的情况下为“学生”。

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

那应该可以解决您的问题。让我知道是否有。

于 2015-06-16T07:03:05.077 回答
1

如果你想使用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());
}

您应该查看ListIteratorIterator的 Javadoc,以了解和之间的区别ListIterator

ListIterator你可以

  • 向后迭代。
  • 随时获取索引。
  • 在任何时候添加一个新值。
  • 在该点设置一个新值。
于 2015-06-16T08:33:37.013 回答
0

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

/* 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());
 }
于 2015-06-16T08:00:49.177 回答