在Java中创建一个列表的列表/对象。

huangapple go评论66阅读模式
英文:

Creating a List of Lists/Objects in java

问题

以下是翻译好的内容:

我最近刚开始学习Java,想尝试创建一个列表的列表。在我在互联网上找到的所有示例中,为了将整数列表作为不同元素添加到另一个列表中,都是创建不同的列表。

当我尝试使用单个列表,但在每次添加之前更改其值(如下面的代码所示)时,我得到了以下结果。
我尝试过另一段类似的代码,但这次只使用了一个对象列表。在这种情况下,我也得到了类似的结果。

class Persona
{
    int num;
    String name;

    public String toString() 
    { return("ID: "+num+" , Name: "+name); }

    public Persona(int num, String name) {
        this.num = num;
        this.name = name; }

    public void set(int num,String name) {
        this.num = num;
        this.name = name;
    }
}

public class Trials {

    public static void main(String[] args) {
        // 情况1:尝试使用列表的列表
        
        List< List< Integer> > collection = new LinkedList<>();

        List<Integer> triplet = new LinkedList<>();

        triplet.add(1);
        triplet.add(3);
        triplet.add(5);

        collection.add(triplet);
        
        System.out.println(collection);
        triplet.clear();

        triplet.add(30);
        triplet.add(65);
        triplet.add(56);
        collection.add(triplet);

        System.out.println(collection); 

        // 情况2:尝试使用对象的列表

        Persona p1 = new Persona(2,"Amy");

        List< Persona > people = new LinkedList<>();

        people.add(p1);
        System.out.println(people);

        p1.set(4, "Jake");

        people.add(p1);
        System.out.println(people);

        /* 输出:
        [[1, 3, 5]]
        [[30, 65, 56], [30, 65, 56]]
        [ID: 2 , Name: Amy]
        [ID: 4 , Name: Jake, ID: 4 , Name: Jake]
        */

    }

}

这是否意味着当处理对象作为列表元素时,它们会被引用?并且,是否有办法使代码在相同的对象/列表上工作,以产生以下期望的输出?

[[1, 3, 5], [30, 65, 56]]
[ID: 2 , Name: Amy, ID: 4 , Name: Jake]

英文:

I just recently started learning Java and wanted to try creating a list of lists. In all the examples I've come across on the internet, inorder to add lists of integers as different elements to another list,different lists were created.

When tried using a single list but with changing it's values each time before adding them ((as shown in the code below), I am getting the following result.
I tried doing another similar code, but this time with just a single list of objects. And in that case also, I am getting a similar result.

class Persona
{
int num;
String name;
public String toString() 
{ return(&quot;ID: &quot;+num+&quot; , Name: &quot;+name); }
public Persona(int num, String name) {
this.num = num;
this.name = name; }
public void set(int num,String name) {
this.num = num;
this.name = name;
}
}
public class Trials {
public static void main(String[] args) {
//CASE 1 : TRYING WITH A LIST OF LISTS
List&lt; List&lt; Integer&gt; &gt; collection = new LinkedList&lt;&gt;();
List&lt;Integer&gt; triplet = new LinkedList&lt;&gt;();
triplet.add(1);
triplet.add(3);
triplet.add(5);
collection.add(triplet);
System.out.println(collection);
triplet.clear();
triplet.add(30);
triplet.add(65);
triplet.add(56);
collection.add(triplet);
System.out.println(collection); 
//CASE 2 : TRYING WITH LIST OF OBJECTS
Persona p1 = new Persona(2,&quot;Amy&quot;);
List&lt; Persona &gt; people = new LinkedList&lt;&gt;();
people.add(p1);
System.out.println(people);
p1.set(4, &quot;Jake&quot;);
people.add(p1);
System.out.println(people);
/*OUTPUT:-
[[1, 3, 5]]
[[30, 65, 56], [30, 65, 56]]
[ID: 2 , Name: Amy]
[ID: 4 , Name: Jake, ID: 4 , Name: Jake]
*/
}
}

Does this mean that when dealing with objects as elements of list, it references them? And also, is there any way of making the code work with the same object/list to give the desirable outputs as follows?

[[1, 3, 5], [30, 65, 56]]
[ID: 2 , Name: Amy, ID: 4 , Name: Jake]

答案1

得分: 0

看起来你想要一个包含两种不同元素的列表,但每种情况下只创建一个元素。

List<Persona> people = new LinkedList<>();

// 创建 p1(Amy)
Persona p1 = new Persona(2, "Amy");

// 将 p1(Amy)添加到 people,现在 people 是 [p1(Amy)]
people.add(p1);

// [p1(Amy)]
System.out.println(people);

// 将 p1 的名字设置为 Jake,由于它是同一个对象,列表中的内容也会更新
// 所以现在 people 是 [p1(Jake)]
p1.set(4, "Jake");

// 将 p1 再次添加到列表中,现在 people 是 [p1(Jake),p1(Jake)]
people.add(p1);

// [p1(Jake),p1(Jake)]
System.out.println(people);

你可能想要做的是,将对象的 修改 p1.set(4, "Jake") 替换为对象的 创建 p1 = new Persona(4, "Jake"),这样你就可以继续进行了。

英文:

Looks like you want to have a List with two different elements, yet create only one in each case.

List&lt; Persona &gt; people = new LinkedList&lt;&gt;();

// Create p1 (Amy)
Persona p1 = new Persona(2, &quot;Amy&quot;);

// Add p1 (Amy) to people, now people is [p1 (Amy)]
people.add(p1);

// [p1 (Amy)]
System.out.println(people);

// Set p1&#39;s name to Jake, it also updates in the list because it is the same object
// so now people is [p1 (Jake)]
p1.set(4, &quot;Jake&quot;);

// Add p1 to list one more time, now people is [p1 (Jake), p1 (Jake)]
people.add(p1);

// [p1 (Jake), p1 (Jake)]
System.out.println(people);

What you might want to do is replacing object modification p1.set(4, &quot;Jake&quot;) with object creation p1 = new Person(4, &quot;Jake&quot;), and then you will be good to go.

huangapple
  • 本文由 发表于 2020年9月25日 00:13:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/64050360.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定