简体   繁体   English

在 Java 中,如何创建一个 ArrayList,该列表中的一个元素是另一个 ArrayList,每个元素都是唯一的?

[英]In Java, how do you create an ArrayList, with one element of that list being another ArrayList, that is unique for each element?

So for example.举个例子。

I have an ArrayList of people.我有一个ArrayList的人。 Created through a people object that contains a name, address, age, etc.通过包含姓名、地址、年龄等的人员 object 创建。

How would I then add another list to that, allowing a unique list of hobbies for each person?然后,我将如何向其中添加另一个列表,从而为每个人提供唯一的爱好列表?

So I could have:所以我可以:

  • James, 32, England, (Football, Tennis)詹姆斯,32 岁,英格兰,(足球、网球)
  • Chloe, 21, Wales, (Art) Chloe,21 岁,威尔士,(艺术)

Tried a few things and struggling with it.尝试了一些事情并与之斗争。

import java.util.ArrayList;

public class People {
    int id;
    String name;
    ArrayList<String> hobbies;

    public People(int id, String name, ArrayList<String> hobbies) {
        this.id = id;
        this.name = name;
        this.hobbies = hobbies;
    }

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

    public People() {
        // TODO Auto-generated constructor stub
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return "People [id=" + id + ", name=" + name + ", hobbies=" + hobbies + "]";
    }
}

import java.util.ArrayList;

public class Runner {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ArrayList<People> arrayPeople = new ArrayList<>();
        ArrayList<String> hobbies = new ArrayList<>();
        hobbies.add("Football");
        hobbies.add("Tennis");
        arrayPeople.add(new People(1,"Paul", hobbies));
        hobbies.add("Golf");
        arrayPeople.add(new People(2,"James", hobbies));

        System.out.println(arrayPeople);
    }
}

This creates a hobby list that is the same for each person, not unique.这将为每个人创建一个相同的爱好列表,而不是唯一的。

This creates a hobby list that is the same for each person, not unique.这将为每个人创建一个相同的爱好列表,而不是唯一的。

That's because member hobbies in [ Paul ] People object has same value as member hobbies in [ James ] People object, since they are assigned the same value in method main of class Runner .这是因为 [ Paul ] People object 中的成员hobbies与 [ James ] People object 中的成员hobbies具有相同的值,因为它们在Runner的方法main中分配了相同的值。 Hence when you change hobbies variable, in method main of class Runner , you are changing for both Paul and James .因此,当您更改hobbies变量时,在 class Runnermain方法中,您正在为PaulJames进行更改。

The simplest solution is to change the class constructor so that it creates a copy of the hobbies parameter and assigns the copy to the hobbies member:最简单的解决方案是更改 class 构造函数,以便它创建hobbies参数的副本并将副本分配给hobbies成员:

public People(int id, String name, List<String> hobbies) {
    this.id = id;
    this.name = name;
    this.hobbies = new ArrayList<>(hobbies);
}

However, I suggest that you add methods to class People to manipulate hobbies member, including:但是,我建议你在 class People中添加方法来操纵hobbies成员,包括:

  • addHobby for adding a hobby addHobby用于添加爱好
  • removeHobby for removing a hobby removeHobby删除爱好
  • clearHobbies for removing all hobbies clearHobbies删除所有爱好
  • getHobbies that returns a copy of hobbies (so that code that calls the method cannot change hobbies ) getHobbies返回hobbies的副本(因此调用该方法的代码不能更改hobbies

Below code demonstrates.下面的代码演示。
Note that you should always use the interface – in this case java.util.List – rather than the implementation – in this case ArrayList – in the API so that you can change class People without having to change its API. Note that you should always use the interface – in this case java.util.List – rather than the implementation – in this case ArrayList – in the API so that you can change class People without having to change its API. If you change the API of class People then all other classes that use class People – like class Runner in the code in your question – will need to be changed as well. If you change the API of class People then all other classes that use class People – like class Runner in the code in your question – will need to be changed as well.

import java.util.ArrayList;
import java.util.List;

public class People {
    private int id;
    private String name;
    private List<String> hobbies;

    public People(int id, String name, List<String> hobbies) {
        this.id = id;
        this.name = name;
        this.hobbies = new ArrayList<>(hobbies);
    }

    public People(int id, String name) {
        this(id, name, new ArrayList<String>());
    }

    public People() {
        this(0, "");
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public void addHobby(String hobby) {
        if (!hobbies.contains(hobby)) {
            hobbies.add(hobby);
        }
    }

    public void clearHobbies() {
        hobbies.clear();
    }

    public List<String> getHobbies() {
        return List.of(hobbies.toArray(new String[]{}));
    }

    public void removeHobby(String hobby) {
        if (hobbies.contains(hobby)) {
            hobbies.remove(hobby);
        }
    }

    @Override
    public String toString() {
        return "People [id=" + id + ", name=" + name + ", hobbies=" + hobbies + "]";
    }

    public static void main(String[] args) {
        List<People> arrayPeople = new ArrayList<>();
        People paul = new People(1,"Paul");
        paul.addHobby("Football");
        paul.addHobby("Tennis");
        People james = new People(2,"James");
        james.addHobby("Football");
        james.addHobby("Tennis");
        james.addHobby("Golf");
        arrayPeople.add(paul);
        arrayPeople.add(james);

        System.out.println(arrayPeople);
    }
}

Running the above code prints the following:运行上面的代码会打印以下内容:

[People [id=1, name=Paul, hobbies=[Football, Tennis]], People [id=2, name=James, hobbies=[Football, Tennis, Golf]]]

The question seemed unclear to me, however I assume that you created lists such as这个问题对我来说似乎不清楚,但是我假设您创建了诸如

[name,age,location]

However, this is not an object .但是,这不是object If you create a person object , you can add features inside it.如果您创建一个人 object ,您可以在其中添加功能。 So that when you create a person object, then you will have access to add/edit new features.这样当您创建一个人 object 时,您就可以访问添加/编辑新功能。 In your case, your features must be:在您的情况下,您的功能必须是:

  • Name姓名
  • Age年龄
  • Location地点
  • List (Whatever you name it, type of it must be an arraylist).列表(无论您如何命名,它的类型都必须是数组列表)。

To have a list of people:要列出人员列表:

class Person{
    String name;
    int age;
    String Address;
    ...
}

and ArrayList<Person>ArrayList<Person>

For the people class, if you need each hobby in hobbies to be unique you can have a Set class to store hobbies.对于 class 人来说,如果您需要爱好中的每个爱好都是独一无二的,您可以拥有一个Set class 来存储爱好。

class Person{
    String name;
    int age;
    String address;
    Set<String> hobbies;
    ...
}

If the order does not matter you can use HashSet To maintain the order you can use TreeSet or LinkedHashSet.如果顺序无关紧要,可以使用 HashSet 来维护顺序,可以使用 TreeSet 或 LinkedHashSet。

class person{
    String name;
    int age;
    String address;
    TreeSet<String> hobbies;
    ...
}
class Person{
    String name;
    int age;
    String address;
    LinkedHashSet<String> hobbies;
    ...
}

To add a hobby to a person.给一个人增加一个爱好。

String hobby = "a hobby";
person.add(hobby);

To add hobbies to a person;给一个人增加爱好;

String hobby1 = "hobby1";
String hobby1 = "hobby2";
...
Set<String> hobbies = new TreeSet(); // or Set<String> hobbies = new LinkedHashMap();
hobbies.add(hobby1);
hobbies.add(hobby2);
...
person.addAll(hobby);

For another person with the same hobbies, you need to copy the hobbies, then modifying the hobbies of the second person will not affect the hobbies of the first person.另一个人有相同的爱好,需要复制爱好,然后修改第二个人的爱好不会影响第一个人的爱好。

Set<String> new_hobbies = new TreeSet(old_hobbies); // or new LinkedHashSet(old_hobbies);
another_person.addAll(new_hobbies);

暂无
暂无

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

相关问题 如何在没有来自另一个 arraylist 的一个元素的情况下创建 arraylist - How to create arraylist without one element from another arraylist 您将如何检查Java ArrayList中每个元素的最后两位数字? - How would you check the last 2 digits of each element in an ArrayList, Java? 如何读取文件,每一行都是Java中ArrayList中的元素? - How do I read in a file, each line being an element in an ArrayList in Java? java-如何为每个子类制作一个ArrayList? - java - how do you make one ArrayList for each subclass? 如何在java中将元素的值从一个arraylist分配和存储到另一个arraylist - How to assign and store value of element from one arraylist to another in java Java-如何将数组的每个元素复制到ArrayList? - Java - How to copy each element of array to ArrayList? 如何从 java 中的另一个 ArrayList 中删除 ArrayList 的元素? - How can I delete an element of an ArrayList from another ArrayList in java? 如何将 ArrayList 中的最后一个元素存储到另一个 ArrayList (Java) - How to store the last element in an ArrayList into another ArrayList (Java) 将ArrayList的每个元素添加到另一个ArrayList的每个元素 - Add each element of an ArrayList to each element of another ArrayList 如何比较一个ArrayList中的element(char)位置比Java中另一个ArrayList中其左侧元素的位置大 - How to compare an element(char) position in one ArrayList is bigger position than its left elements in another ArrayList in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM