简体   繁体   English

从类的数组列表中获取变量的数组列表

[英]Getting arraylist of variable from arraylist of class

I have an arraylist of class Person 我有一个类Person的数组列表

ArrayList<Person> people = new ArrayList<Person>();

Inside that Person class i have some variables associated with that person. 在那个Person类中,我有一些与那个人相关的变量。

public class Person {
    String Name;
    String Address;
    String Phonenumber;
}

Now is there a way using the people arraylist to get an arraylist of Name? 现在有一种方法可以使用people数组列表来获取Name的数组列表?

You have to iterate over it. 您必须对其进行迭代。

List<String> names = new ArrayList<>();

for(Person person : people) {
   names.add(person.getName()); // Assuming you have a getter
}

Now is there a way using the people arraylist to get an arraylist of Name? 现在有一种方法可以使用people数组列表来获取Name的数组列表?

Yes . 是的 Define a name arraylist as ArrayList<String> . 将名称arraylist定义为ArrayList<String> Than, iterate over ArraList<Person> and put value on name arraylist. 然后,遍历ArraList<Person>并将值放在名称arraylist上。

List<String> nameList = new ArrayList<>();

for(Person person : people) {
   nameList.add(person.getName());
}

If you really want to maintain a pair of ArrayLists, one containing Person objects, and the other containing the Name property of the person objects, you could always create another class: 如果您确实要维护一对ArrayList,其中一个包含Person对象,另一个包含person对象的Name属性,则可以始终创建另一个类:

public class PersonList {
    private ArrayList<Person> people;
    private ArrayList<String> names;

    public PersonList() {
        people = new ArrayList<>();
        names = new ArrayList<>();
    }

    public PersonList(ArrayList<Person> p) {
        people = p;
        names = new ArrayList<>();
        for(Person person : p) {
            names.add(p.getName());
        }
    }

    public ArrayList<Person> getPeople() {
        return people;
    }

    public ArrayList<String> getNames() {
        return names;
    }

    public void add(Person p) {
        people.add(p);
        names.add(p.getName());
    }

    public Person getPerson(int index) {
        return people.get(index);
    }

    public String getName(int index) {
        return names.get(index);
    }

    // more methods to continue to replicate the properties of ArrayList...
    // just depends what you need
}

You will have to continue adding methods to this class so that this class can do everything that you can do on a single ArrayList . 您将必须继续向此类添加方法,以便此类可以完成您在单个ArrayList上可以执行的所有操作。 And really, this class is just a convenient way of making it easier to work maintain two different ArrayLists at the same time. 确实,此类只是一种便捷的方法,可以使同时维护两个不同的ArrayLists变得更容易。

Anyway, now in your main code, you can instantiate an object of PersonList instead of an array list: 无论如何,现在在您的主代码中,您可以实例化PersonList的对象而不是数组列表:

PersonList people = new PersonList();

And add to people . 并加people Or you could even just use a regular array list up until you need the names list, and instantiate a PersonList with the other constructor I provided: 或者甚至可以只使用常规的数组列表,直到需要名称列表,然后使用我提供的其他构造函数实例化PersonList

// Assuming ArrayList<People> arrPeople exists...
PersonList people = new PersonList(arrPeople);

And now the people object will contain an ArrayList identical to arrPeople , as well as a matching names list containing all the names. 现在, people对象将包含一个与arrPeople相同的ArrayList ,以及一个包含所有名称的匹配names列表。 And no matter how you instantiate a PersonList , calling the add method on a PersonList (same syntax as an ArrayList if you set this class up correctly), and it will keep both array lists that the class manages in sync. 而且不管你如何实例化一个PersonList ,调用add的方法PersonList (相同的语法,一个ArrayList ,如果你正确设置该类了),它会保留两个数组列出了类的同步管理。

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

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