Bob
Bob

Reputation: 592

Java ArrayList clear() method

I can't really understand the code below. The clear method removes all elements assigned to "al" right? Then the ArrayList al receives the value of string Str2 and alal receives the value of Str3.

So how on earth the output is as below if alal is assigned only with Str3? Could someone kindly explain? Many thanks

OUTPUT:

[String 2]

[[String 2], String 3]

public static void main(String[] args){

    String Str1 = "String 1";
    String Str2 = "String 2";
    String Str3 = "String 3";


    ArrayList al = new ArrayList();
    ArrayList alal = new ArrayList();


    al.add(Str1);
    alal.add(al);

    al.clear();

    al.add(Str2);
    alal.add(Str3);

    System.out.println(al);
    System.out.println(alal);
}

Upvotes: 1

Views: 307

Answers (6)

s16h
s16h

Reputation: 4855

Let's consider what happens line by line:

1.

al.add(Str1);

al now contains the String "String 1" so it looks like: ["String 1"]

2.

alal.add(al);

alal now contains al (al currently is ["String 1"]). So, alal looks like: [["String 1"]]

3.

al.clear();

Since the content of al is cleared, it now looks like: [] But alal also has a reference to al so alal will look like: [[]]

4.

al.add(Str2);

al will now look like: ["String 2"] As we know, alal also has a reference to al so alal will look like: [["String 2"]]

5.

alal.add(Str3);

Finally, we add "String 3" to alal (which is currrently [["String 2"]]), so it becomes: [["String 2"], "String 3"]

Finally

Now al is ["String 2"] and, alal is [["String 2"], "String 3"]

Upvotes: 0

Luiggi Mendoza
Luiggi Mendoza

Reputation: 85779

You're using raw ArrayList, thus you can store any kind of Object in your lists. Note that you're storing al inside alal, which means storing the reference of al into alal, not the values from al into alal.

More in depth explanation:

al.add(Str1); //al -> [String 1]
alal.add(al); //alal -> [ [String 1] ]

al.clear(); //al -> []   ,   alal -> [ [] ]
al.add(Str2); // al -> [String 2] , alal -> [ [String 2] ]
alal.add(Str3); //alal -> [ [String 2], String 3]

System.out.println(al);
System.out.println(alal);

Some advices from your given code:

  • Use better names for your variables. al and alal may look good for a basic example, but it would be better providing names that help to the code readability.
  • If you want to prevent you or another programmer to store anything in an ArraList (or in another Collection), use generics. This mean, declare the variable like ArrayList<String> alal so now the compiler will check that you can add String values only.
  • If you wanted to pass the elements from al to alal, use ArrayList#addAll method.
  • In this case, it would be better declaring the variables as List instead to ArrayList. It is better to get used to program to an interface rather than a direct class implementation:

Upvotes: 7

Benjamin RD
Benjamin RD

Reputation: 12034

First, in your code, you are declaring 3 String variables (Str1, Str2 and Str3) and 2 ArrayList (al and alal).

Now, you are adding the element Str1 to your list al with:

al.add(Str1); //Here your variable al has the value ["String1"]

And when you assign

alal.add(al); // Now your variable alal has the value [["String1"]]

and when you add the new element to your variable alal the result is

alal.add(Str3); // Now you are adding the string to your array, and the result is `[["String1"],"String 3"]`

If you need remove the result before to assignee a new result you need do a .clear to your alal variable only to contains a string array and not arrays of string.

Upvotes: 0

When you are doing alal.add(al);, you are not copying anything from al into alal, you are actually making the first element of alal be al (you are storing a list into a list, which is allowed). When you clear al, you remove every element from it, but do not create a new list (that would no longer be in alal. When you go to print alal, you are printing every element in it. The first element is the list al, which prints as [String 2] (and then the String 3).

Upvotes: 0

DerStrom8
DerStrom8

Reputation: 1341

You never cleared alal, so it still contains al when you add Str3.

Upvotes: 0

Gautam
Gautam

Reputation: 3386

because alal has reference of al

Any change made to al reference will reflect in alal

Upvotes: 0

Related Questions