Creating a List of Lists/Objects in java

Merin Cherian

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("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) {
       //CASE 1 : TRYING WITH A LIST OF LISTS
        
        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); 

      //CASE 2 : TRYING WITH LIST OF OBJECTS

        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);

      /*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]

DelfikPro

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

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

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

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

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

// Set p1'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, "Jake");

// 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, "Jake") with object creation p1 = new Person(4, "Jake"), and then you will be good to go.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Creating a list of Exceptions in java

From Dev

Java - Creating an object extending List

From Dev

Creating and printing user list in JAVA

From Dev

Creating a List of TableColumn, java swing

From Dev

stream creating List of List (nested List) using forEach, Java 8

From Java

Error about creating array of Generic List in Java

From Dev

Creating List<Integer> with all same values in JAVA

From Dev

Creating a contact list with java and object oriented

From Dev

Creating Object List in Java class - better approach

From Dev

Error about creating array of Generic List in Java

From Dev

Java. Creating new class similar to List

From Dev

Creating a contact list with java and object oriented

From Dev

Creating Object List in Java class - better approach

From Dev

creating list from java 8 stream api

From Dev

java-8 filter a list without creating a new list

From Java

Creating a map of String and Object from a List in Java 8

From Java

Java streams for adding to a list if it exists or creating a new one in a HashMap

From Dev

How to get back to the head of a singly linked list after creating it in java?

From Dev

Creating a map of String and Object from a List in Java 8

From Dev

creating nested maps with nested properties from List, stream, Java 8

From Dev

Creating a 2 dimensional list using java 8 streams

From Dev

java.lang.ClassCastException: Creating a synchronized Linked List

From Dev

Output error while creating Java Json array object with List and Hashmap

From Dev

Java 8: Idiomatically creating a Comparator for ordering objects based on their index in a List

From Dev

Linked List creating a List

From Dev

Freemarker: creating a table with a list in a list

From Java

Creating adjacency List Graph

From Dev

Creating an Edge List in Stata

From Dev

Creating Large List<T>

Related Related

  1. 1

    Creating a list of Exceptions in java

  2. 2

    Java - Creating an object extending List

  3. 3

    Creating and printing user list in JAVA

  4. 4

    Creating a List of TableColumn, java swing

  5. 5

    stream creating List of List (nested List) using forEach, Java 8

  6. 6

    Error about creating array of Generic List in Java

  7. 7

    Creating List<Integer> with all same values in JAVA

  8. 8

    Creating a contact list with java and object oriented

  9. 9

    Creating Object List in Java class - better approach

  10. 10

    Error about creating array of Generic List in Java

  11. 11

    Java. Creating new class similar to List

  12. 12

    Creating a contact list with java and object oriented

  13. 13

    Creating Object List in Java class - better approach

  14. 14

    creating list from java 8 stream api

  15. 15

    java-8 filter a list without creating a new list

  16. 16

    Creating a map of String and Object from a List in Java 8

  17. 17

    Java streams for adding to a list if it exists or creating a new one in a HashMap

  18. 18

    How to get back to the head of a singly linked list after creating it in java?

  19. 19

    Creating a map of String and Object from a List in Java 8

  20. 20

    creating nested maps with nested properties from List, stream, Java 8

  21. 21

    Creating a 2 dimensional list using java 8 streams

  22. 22

    java.lang.ClassCastException: Creating a synchronized Linked List

  23. 23

    Output error while creating Java Json array object with List and Hashmap

  24. 24

    Java 8: Idiomatically creating a Comparator for ordering objects based on their index in a List

  25. 25

    Linked List creating a List

  26. 26

    Freemarker: creating a table with a list in a list

  27. 27

    Creating adjacency List Graph

  28. 28

    Creating an Edge List in Stata

  29. 29

    Creating Large List<T>

HotTag

Archive