Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Different outputs while printing ArrayList in java

Review last 2 lines of both outputs why different? I am satisfied with program 1

Program 1

public static void main(String[] args) 
{
    ArrayList al = new ArrayList();
    al.add(10);
    al.add(20);
    al.add(30);
    System.out.println(al);
    System.out.println("-----------------------------------");

    al.addAll(2, al);                                                       
    System.out.println("-----------------------------------");
    System.out.println(al);

    System.out.println(al.get(2).getClass());
}

Output :

[10, 20, 30]
-----------------------------------
-----------------------------------
**[10, 20, 10, 20, 30, 30]
class java.lang.Integer**

I am unsatisfied with program 2 output Why different output while printing arraylist? why "java.util.ArrayList" is last line of output for prog 2 but "java.lang.Integer" for prog1

Program 2

public static void main(String[] args) 
    {
        ArrayList al = new ArrayList();
        al.add(10);
        al.add(20);
        al.add(30);
        System.out.println(al);
        System.out.println("-----------------------------------");

        al.add(2,al);                                                   
        al.add(8);                                                         
        al.add(2);                                                      
        al.add(4);                                                      
        al.add(1);                                                      
        System.out.println("-----------------------------------");
        System.out.println(al);
        System.out.println(al.get(2).getClass());
    }

Output : 

[10, 20, 30]
-----------------------------------
-----------------------------------
[10, 20, (this Collection), 30, 8, 2, 4, 1]
class java.util.ArrayList
like image 342
Swapnil Avatar asked Jan 28 '23 15:01

Swapnil


2 Answers

Instead of al.add(2, al);, you almost certainly want al.addAll(2, al);, which will add the contents of al (as it stands before that method call is complete) to al.

At present, you're just adding an ArrayList object to the list, and since your ArrayList can contain any old thing due to lack of generic parameters (not desirable!), this compiles and runs just fine.

You're running into this issue because you're using a generic ArrayList without any generic parameters. If you were explicitly using ArrayList<Integer>, then that line as it stands wouldn't compile. Using it without such parameters is only supported for legacy, backwards compatibility reasons, and should be avoided.

like image 110
Michael Berry Avatar answered Jan 31 '23 08:01

Michael Berry


In Program 2, the line:

al.add(2,al);  

adds the Arraylist 'al' to itself at index 2. The code:

al.get(2)

therefore returns an ArrayList. That's why printing the two lists returns different results.

To avoid issues like this, initialize the ArrayList with: ArrayList<Integer>

like image 30
Don Pauer Avatar answered Jan 31 '23 09:01

Don Pauer