简体   繁体   English

从另一个类调用对象arraylist并遍历它

[英]Calling an object arraylist from another class and iteration through it

I have a problem accessing the arraylist I created in a class. 访问类中创建的数组列表时遇到问题。 I tried going through the answers of questions similar to mine but unfortunately I was unable to solve the problem. 我尝试通过类似我的问题的答案,但是不幸的是我无法解决问题。

So I have two classes Student and Person and I want to iterate through the arraylist of Person in the class Student . 所以我有两个类StudentPerson ,我想遍历Student类中Person的数组列表。 (The code doesn't really make sense, I know. I just want to understand). (我知道代码没有什么意义。我只想了解)。

I tried two approaches : 我尝试了两种方法:

1) creating a variable of type Person in Student class and calling the get method from person class. 1)在Student类中创建一个Person类型的变量,并从person类中调用get方法。

2) creating a get method in the class person that returns arraylist. 2)在返回arraylist的类人员中创建get方法。 Both are not working properly when i tried to call the isHere method in the main method.(false was printed instead of true) 当我尝试在main方法中调用isHere方法时,两者均无法正常工作。(打印的是false而不是true)

I think my two approaches intialise a new array of type Person and not call the arraylist to which elements are already added. 我认为我的两种方法会初始化一个Person类型的新数组,而不是调用已经添加了元素的arraylist。 How can solve this? 怎么解决呢?

import java.util.ArrayList;

public class Student {
    private Person p;
    private String name;
    private int age;
    private String address;

    public Student() {

    }

    public Student(String name, int age, String address) {
        this.name = name;
        this.age = age;
        this.address = address;
    }

    public boolean isHere(String name) {
        p = new Person();
        // I also tried for(Person per : p.getList)
        for (Person per : this.getL()) {
            if (per.getName().equals(name)) {
                System.out.println("hi");
                return true;
            }
        }
        return false;
    }

    public ArrayList<Person> getL() {
        return p.getList();
    }

    public Person getP() {
        return p;
    }

    public void setP(Person p) {
        this.p = p;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

}

The Person class: Person类:

import java.util.ArrayList;

public class Person {
    private String name;
    private ArrayList<Person> list = new ArrayList<Person>();

    Person() {

    }

    public Person(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public ArrayList<Person> getList() {
        return list;
    }

    public void setList(ArrayList<Person> list) {
        this.list = list;
    }

    public void add(String name) {
        list.add(new Person(name));
    }
}

The statement p = new Person() in your isHere(..) method is creating a new Person object. isHere(..)方法中的语句p = new Person()正在创建新的Person对象。 When that Person object is created, the name in object p will be null and the list will be empty. 创建该Person对象时,对象pname将为null, list将为空。 So the for loop is never executed as the list is empty and hence it returns false. 因此,因为列表为空,所以永远不会执行for循环,因此它返回false。

If you ever want your code to run, you should not create the Person object and then immediately iterate through it because it will have nothing. 如果您要运行代码,则不应创建Person对象,然后立即对其进行迭代,因为它将没有任何内容。 You have to either add something to it or use a Person object which you believe will be populated before you run the isHere(..) method 您必须在其中添加某些内容,或者使用您认为将在运行isHere(..)方法之前填充的Person对象isHere(..)

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM