[英]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:所以我可以:
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 Runner
的main
方法中,您正在为Paul和James进行更改。
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:在您的情况下,您的功能必须是:
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.