ArrayList vs. Arrays.asList(array)

I'm confused about the output I am getting. My class is not finished yet but here goes

import java.util.*;

publicclass Test

{

publicstaticvoid main (String [] args)

{

ArrayList<Integer> k =new ArrayList<Integer>();

k.add(1);

k.add(2);

k.add(3);

int [] l ={1,2,3};

System.out.println(TareqTools.permuteList(k));

System.out.println(TareqTools.permuteList(l));

}

}

The output is

[[[1, 2, 3]]]

[[[I@82ba41]]

The first output is from the ArrayList, the second is from the array.

At the moment, permuteList

is just printing the ArrayList.

My question is basically: Why is there a difference between the visual output of an ArrayList and the visual output of an array fed into a List?

Thanks in advance

[1275 byte] By [tkera] at [2007-10-2 5:39:41]
# 1

> My question is basically: Why is there a difference

> between the visual output of an ArrayList and the

> visual output of an array fed into a List?

>

> Thanks in advance

Because ArrayList.toString() has been defined to do that.

A plain old array has no overridden toString() method, so it just inherits the implementation that Object defined.

warnerjaa at 2007-7-16 1:50:01 > top of Java-index,Java Essentials,Java Programming...
# 2
So, if I am following you correctly, even if an array is fed into a List, in order for it to make sense (to humans), I would have to write code that takes each element of the said list and formats it appropriately to make it human-readable.Thanks.
tkera at 2007-7-16 1:50:01 > top of Java-index,Java Essentials,Java Programming...
# 3

> So, if I am following you correctly, even if an array

> is fed into a List, in order for it to make sense (to

> humans), I would have to write code that takes each

> element of the said list and formats it appropriately

> to make it human-readable.

>

> Thanks.

Well, your subject line seems to indicate you did an Arrays.asList(l) somewhere. If you did that, then printing the returned List (explicitly or implicitly invoking the toString() method) should format it like [1, 2, 3]. I have to guess you actually just printed the array itself (l) to get the output you received.

warnerjaa at 2007-7-16 1:50:01 > top of Java-index,Java Essentials,Java Programming...
# 4

Actually, no. The permuteList method returns ArrayLists, not arrays. I never called the toString() method for anything. However, I found an easier way to solve the problem. Basically, I use a for loop and ArrayList's add method to just add each element in the normal array.

Still feel kind of disappointed though. Maybe what I really want is an ArrayList constructor that takes in an array as an argument.

tkera at 2007-7-16 1:50:01 > top of Java-index,Java Essentials,Java Programming...
# 5
You're saying that this:int[] intArray = new int[] { 1, 2, 3 };List list = Arrays.asList(intArray);System.out.println(list);doesn't print something like "[1, 2, 3]" ?I believe it does (I'm not running it to test though).
warnerjaa at 2007-7-16 1:50:01 > top of Java-index,Java Essentials,Java Programming...
# 6
No. It printed the other output.
tkera at 2007-7-16 1:50:01 > top of Java-index,Java Essentials,Java Programming...
# 7

> No. It printed the other output.

I believe you are not actually running something resembling what I posted to try. Because when I do it, it works just fine (except since I only have Java 1.4.2 without generics and autoboxing, I had to create an array of Integer objects instead of ints).

Integer[] x = new Integer[] { new Integer(1), new Integer(2), new Integer(3) };

List l = Arrays.asList(x);

System.out.println("list: " + l); // prints list: [1, 2, 3]

System.out.println("original array: " + x); // prints the 'strange' output

warnerjaa at 2007-7-16 1:50:01 > top of Java-index,Java Essentials,Java Programming...
# 8

one thing i like about java is it's extensive library which is full of toys...

you can use java.util.Arrays' toString() method to get the same output for a primitive array as you do a List / ArrayList....

import java.util.*;

public class PrintArray

{

public static void main (String [] args)

{

ArrayList<Integer> k = new ArrayList<Integer>();

k.add(1);

k.add(2);

k.add(3);

int [] l = {1,2,3};

System.out.println(k);

System.out.println(Arrays.toString(l)); // using java.util.Arrays' static toString() method

}

}

sure beats having to code all that... :-p

have fun... :-)

- MaxxDmg...

- ' He who never sleeps... '

MaxxDmga at 2007-7-16 1:50:01 > top of Java-index,Java Essentials,Java Programming...
# 9

No seriously, when I do the Array.asList(x) thing I get the 'strange' output.

I even tried this:

import java.util.*;

public class Test

{

public static void main (String [] args)

{

int [] l = {1,2,3};

System.out.println(Arrays.asList(l));

}

}

tkera at 2007-7-16 1:50:01 > top of Java-index,Java Essentials,Java Programming...
# 10

wow... that was quite interesting...

Arrays.asList() returns it's own private version of ArrayList which is a private static class called ArrayList... ( Arrays.ArrayList )...

private static class ArrayList<E> extends AbstractList<E>

implements RandomAccess, java.io.Serializable

{

private static final long serialVersionUID = -2764017481108945198;

private Object[] a;

ArrayList(E[] array) {

if (array==null)

throw new NullPointerException();

a = array;

}

and looks like it's broken if using primatives...

import java.util.*;

public class PrintArray

{

public static void main (String [] args)

{

Integer[] x = new Integer[] { new Integer(1), new Integer(2), new Integer(3) };

List l = Arrays.asList(x);

System.out.println("list: " + l);

int [] g = {1,2,3};

List al = Arrays.asList(g);

Object[] oarray = al.toArray();

System.out.println(" Object Array Element:0 " + oarray[0]);

Object o = al.get(0);

Class c = o.getClass();

System.out.println(" List Element: 0 " + al.get(0));

System.out.println(" Data Type for Element: 0 " + c.getSimpleName());

o = al.get(1); // throws an ArrayIndexOutOfBoundsException

c = o.getClass();

System.out.println(" List Element: 1 " + al.get(1));

System.out.println(" Data Type for Element: 1 " + c.getSimpleName());

System.out.println(" Displays List " +al);

}

}

// results...

C:\Java\Stuff2>java PrintArray

list: [1, 2, 3]

Object Array Element:0 [I@10b62c9

List Element: 0 [I@10b62c9

Data Type for Element: 0 int[]

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

at java.util.Arrays$ArrayList.get(Unknown Source)

at PrintArray.main(PrintArray.java:18)

the reason why it displays junk is because the first element is the primative array one passes to the method... quite interesting...

will be submitting a bug report... :-)

- MaxxDmg...

- ' He who never sleeps... '

MaxxDmga at 2007-7-16 1:50:01 > top of Java-index,Java Essentials,Java Programming...
# 11

wow... that was quite interesting...

Arrays.asList() returns it's own private version of ArrayList which is a private static class called ArrayList... ( Arrays.ArrayList )...

private static class ArrayList<E> extends AbstractList<E>

implements RandomAccess, java.io.Serializable

{

private static final long serialVersionUID = -2764017481108945198;

private Object[] a;

ArrayList(E[] array) {

if (array==null)

throw new NullPointerException();

a = array;

}

and looks like it's broken if using primatives...

import java.util.*;

public class PrintArray

{

public static void main (String [] args)

{

Integer[] x = new Integer[] { new Integer(1), new Integer(2), new Integer(3) };

List l = Arrays.asList(x);

System.out.println("list: " + l);

int [] g = {1,2,3};

List al = Arrays.asList(g);

Object[] oarray = al.toArray();

System.out.println(" Object Array Element:0 " + oarray[0]);

Object o = al.get(0);

Class c = o.getClass();

System.out.println(" List Element: 0 " + al.get(0));

System.out.println(" Data Type for Element: 0 " + c.getSimpleName());

o = al.get(1); // throws an ArrayIndexOutOfBoundsException

c = o.getClass();

System.out.println(" List Element: 1 " + al.get(1));

System.out.println(" Data Type for Element: 1 " + c.getSimpleName());

System.out.println(" Displays List " +al);

}

}

// results...

C:\Java\Stuff2>java PrintArray

list: [1, 2, 3]

Object Array Element:0 [I@10b62c9

List Element: 0 [I@10b62c9

Data Type for Element: 0 int[]

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 1

at java.util.Arrays$ArrayList.get(Unknown Source)

at PrintArray.main(PrintArray.java:18)

the reason why it displays junk is because the first element is the primative array one passes to the method... quite interesting...

will be submitting a bug report... :-)

- MaxxDmg...

- ' He who never sleeps... '

MaxxDmga at 2007-7-16 1:50:01 > top of Java-index,Java Essentials,Java Programming...
# 12
lol... double reply due to the forum acting funny... :-p- MaxxDmg...- ' He who never sleeps... '
MaxxDmga at 2007-7-16 1:50:01 > top of Java-index,Java Essentials,Java Programming...
# 13

int[] a = new int[]{1,2,3};

System.out.println(Arrays.asList(a));

Integer[] b= new Integer[]{new Integer(1),new Integer(2),new Integer(3)};

System.out.println(Arrays.asList(b));

jdk5 autoboxing not work here!

output is:

[[I@35ce36]

[1, 2, 3]

neoedmunda at 2007-7-16 1:50:01 > top of Java-index,Java Essentials,Java Programming...
# 14

I was (and am) a bit suprised about the behavior of Arrays.asList(int[]) in Java 5.0 (1.5.0_11 is what I'm experimenting with).

I just asked basically the same question on comp.lang.java.programmer. Please see the following for some hopefully helpful info:

http://groups.google.com/group/comp.lang.java.programmer/browse_thread/thread/51578c2656a5d24b/87382ab5ec3d8b05

Same URL made tiny: http://tinyurl.com/39sls3

Anyone know if Java 6.0 works differently in this regard?

meonkeysa at 2007-7-16 1:50:01 > top of Java-index,Java Essentials,Java Programming...
# 15

More hopefully helpful posts on this topic from c.l.j.programmer:

Why doesn't Arrays.toList work?

http://groups.google.com/group/comp.lang.java.programmer/browse_thread/thread/f3b20887f3c1b527/5a7e02eb7616fb7d

Using Arrays and Collections

http://groups.google.com/group/comp.lang.java.programmer/browse_thread/thread/2d9142a07324d26d/cbac502b783bf456

Arrays.asList() returning java.util.Arrays$ArrayList

http://groups.google.com/group/comp.lang.java.programmer/browse_thread/thread/e58538d8207269ce/4e94623fb2f98ca1

Why I can not find Arrays.asList(Object [] s)

http://groups.google.com/group/comp.lang.java.programmer/browse_thread/thread/bcf0047de617c390/cb84b2a30ba79455

meonkeysa at 2007-7-20 18:39:57 > top of Java-index,Java Essentials,Java Programming...