Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/350.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/grails/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 类的数组列表的迭代器_Java_Collections_Iterator_Listiterator - Fatal编程技术网

Java 类的数组列表的迭代器

Java 类的数组列表的迭代器,java,collections,iterator,listiterator,Java,Collections,Iterator,Listiterator,我是Java新手,所以如果我的问题看起来很傻,请容忍我 我正在学习集合,我已经编写了一个程序来存储学生姓名、id和标记。我正在将所有这些存储在Arraylist中。 使用for循环,我可以打印程序中的值。 但我不知道如何使用迭代器来实现这一点。 我的代码是这样的 Java public class Student{ private String name; private String rollno; private String biology; private Str

我是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;
   }


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

    }
  }    
}

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

既然你有错误,你就必须明确地抛出它:

 Student student=(Student)itr.next();
或者将ListIterator更改为使用泛型

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

当您拥有它时,您必须显式地强制转换它:

 Student student=(Student)itr.next();
或者将ListIterator更改为使用泛型

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

您缺少ListIterator中的泛型声明

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


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

您缺少ListIterator中的泛型声明

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


没有理由在这个时代开始强制转换。

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

下面的语句假设ListIterator是默认的引用类型,即Object

ListIterator itr=details.listIterator();
通过指定您正在处理的引用对象的确切类型(在您的案例中为“Student”)来更改上述语句

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

那应该能解决你的问题。如果有,请告诉我。

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

下面的语句假设ListIterator是默认的引用类型,即Object

ListIterator itr=details.listIterator();
通过指定您正在处理的引用对象的确切类型(在您的案例中为“Student”)来更改上述语句

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

那应该能解决你的问题。如果有,请告诉我。

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

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

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

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

请说明您将使用的引用对象的类型。 在您的情况下,它将是学生对象

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

请说明您将使用的引用对象的类型。 在您的情况下,它将是学生对象

/* 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<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());
}
为了理解迭代器和ListIterator之间的区别,您应该看一下的Javadoc

ListIterator <Student>itr=details.listIterator();
使用ListIterator,您可以

向后迭代。 在任意点获取索引。 在任意点添加新值。 在该点设置一个新值。 如果您想使用迭代器模式。您的代码应该是:

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());
}
为了理解迭代器和ListIterator之间的区别,您应该看一下的Javadoc

ListIterator <Student>itr=details.listIterator();
使用ListIterator,您可以

向后迭代。 在任意点获取索引。 在任意点添加新值。 在该点设置一个新值。