DroidDroid
DroidDroid

Reputation: 95

Java List of Lists

I'm programming a list, containing lists of strings.

That's how it looks like:

List<List<String>> WPL = new ArrayList<List<String>>();
List<String> WP = new ArrayList<String>();

WP.add("A");
WP.add("B");
WPL.add(WP);

WP.clear();

WP.add("C");
WP.add("D");
WPL.add(WP);

for(List<String> a : WPL) {
    System.out.println(a.indexOf(a));
    for (String i : a)
    {
        System.out.println(i);
    }

}

(The lists will be generated automized later and much longer) All I get as a response is

-1
C
D
-1
C
D

What I need is

0
A
B
1
C
D

What am I doing wrong?

Upvotes: 2

Views: 241

Answers (4)

Andreas
Andreas

Reputation: 159096

Just for kicks, here is the very short version that also fixes your print loop.

List<List<String>> WPL = Arrays.asList(Arrays.asList("A", "B"),
                                       Arrays.asList("C", "D"));
for (int i = 0; i < WPL.size(); i++) {
    System.out.println(i);
    for (String s : WPL.get(i))
        System.out.println(s);
}

Output:

0
A
B
1
C
D

Upvotes: 0

John Smith
John Smith

Reputation: 7407

When you make a list of lists, it isn't copying the values- it's copying the reference TO the source list. So when you

    WP.add("A");
    WP.add("B");
    WPL.add(WP);    

    WP.clear();  //this also clears the values in WPL since WPL doesn't store A and B, it stores a reference to WP.

    WP.add("C");
    WP.add("D");
    WPL.add(WP);

To solve this, I recommend creating a new list for each item that needs to be added into it. But also be careful to keep in mind that any changes to the original source lists will also be reflected in WPL. You could also just set WP to a new instance of an ArrayList (if you need to reference back to the old ArrayList, you could access it via WPL, since WPL is actually storing a reference to it as opposed to the values directly).

    List<List<String>> WPL = new ArrayList<List<String>>();
    List<String> WP = new ArrayList<String>();

    WP.add("A");
    WP.add("B");
    WPL.add(WP);

    WP = new ArrayList<String>();

    WP.add("C");
    WP.add("D");
    WPL.add(WP);

    for(List<String> a : WPL) {
        System.out.println(a.indexOf(a));

        for (String i : a)
        {
            System.out.println(i);
        }

    }

Here is a good video to watch that describes the difference between reference types and value types if you wanted to learn a little more about what is going on (which I would recommend doing.. this is a topic you will probably come across again in the future).

https://www.youtube.com/watch?v=eRfvgSvf-mM

Upvotes: 2

Zaki Anwar Hamdani
Zaki Anwar Hamdani

Reputation: 499

Java works on reference

WP.clear();

is clearing the list where 'A' and 'B' was already added. What you need to do instead is

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

    WP.add("A");
    WP.add("B");
    WPL.add(WP);

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

    WP2.add("C");
    WP2.add("D");
    WPL.add(WP2);

Upvotes: 0

Ari Maniatis
Ari Maniatis

Reputation: 8668

WPL.add(WP)

That adds a reference to the list WP. It does not make a copy of WP. So when you clear WP, you clear the contents of the list inside WPL.

Upvotes: 2

Related Questions