简体   繁体   English

如何迭代在另一个类中声明为 ArrayList 的类对象?

[英]How do I iterate over a class object that is declared as an ArrayList in another class?

So I have 2 classes: ClassRoom and Student .所以我有 2 个班级: ClassRoomStudent

In ClassRoom, I create an Arraylist using a Student type:在 ClassRoom 中,我使用 Student 类型创建了一个 Arraylist:

public class ClassRoom{
    int classID;
    String className;
    ArrayList<Student> studList = new ArrayList<>();

    public ClassRoom(int classID, String className){
        this.classID = classID;
        this.className = className;
    }

    public String toString(){
        return ("Class ID: " + classID + " Class name: " + className + " Students list: " + studList);
    } 

I also have 2 functions.我也有2个功能。 One adds a student to the list and the other sends a message to each student's phone number.一个将学生添加到列表中,另一个向每个学生的电话号码发送一条消息。 This is where I'm kinda stuck.这是我有点卡住的地方。

    public void addStudent(int studID, String studName, int studPhoneNum){
        Student stud = new Student(studName, studID, studPhoneNum);
        studList.add(stud);
        System.out.println(studList);
    }

    public void sendMessage(int studPhoneNum){
        for (Student phone : studList[studPhoneNum]) {
            System.out.println("Sending message to: " + phone);
        }
    }

I'm trying to understand how to iterate over the value studPhoneNum that is in the studList arraylist.我试图了解如何迭代studPhoneNum中的studList值。

I get this error when I'm trying to do what I pasted here: The type of the expression must be an array type but it resolved to ArrayList<Student>当我尝试执行此处粘贴的操作时出现此错误: The type of the expression must be an array type but it resolved to ArrayList<Student>

I'd love to know how to solve this.我很想知道如何解决这个问题。

Thanks谢谢

The answer is simple.答案很简单。 An ArrayList is not an array. ArrayList不是数组。 To access an item in an ArrayList , use [your_array_list].get([your_index])要访问ArrayList的项目,请使用[your_array_list].get([your_index])

So to iterate over an ArrayList you can use所以要迭代一个 ArrayList 你可以使用

for(Student student: studList)
{
  System.out.println("Sending message to: " + student);
}

or或者

for(int i=0; i < studList.size(); ++i)
{
  System.out.println("Sending message to: " + studList.get(i));
}

Consider using Streams to optimize your coding:考虑使用 Streams 来优化您的编码:

public void sendMessage(int studPhoneNum){
  Student student = studList.stream().filter(student-> student.studPhoneNum == studPhoneNum).findFirst().get();
  System.out.println("Sending message to: " + student.studName + " at " + student.studPhoneNum);
}

You can see the entire thing in practice here: https://repl.it/@randycasburn/Stream-Filter?language=java10&folderId=您可以在此处查看实践中的全部内容: https : //repl.it/@randycasburn/Stream-Filter?language=java10&folderId=

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何遍历类成员? - How do I iterate over class members? 如何从ArrayList迭代器迭代Class Object - How to iterate Class Object from an ArrayList iterator 如何将 object 从一个 class 存储到另一个 ZA2F2ED4F8EBC2CBB4C21A29DC40AB6 的 arraylist 中 - How to I store an object in an arraylist from one class in another class 如何在ArrayList中迭代一个类? - How to iterate a class in ArrayList? 如何遍历一个类的方法,然后遍历返回的对象并调用另一个方法? - How to iterate over methods of a class and then iterate over returned object and call another method ? 如何从具有数组列表的类中调用方法到另一个类? - How do I call methods from a class with an arraylist to another class? 如何将 object 从一个 class 分配给另一个 class 以及如何在 Z57A97A39435CFDFED96E03 中计算 double 和 int? - How do I assign object from one class to another class and how do I calculate double and int inside an ArrayList? 如何引用正确声明匿名类的对象? - How do I reference the object an anonymous class is declared in properly? 如何将对象中的 Arraylist 用于另一个类中的方法 - How can I use an Arraylist in an object for a method in another class 如何从一个类的一个arrayList到另一个类的arraylist获取一定数量的对象? - How do I get a certain number of objects from one arrayList in one class to an arraylist in another class?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM