tags:

views:

168

answers:

6
int[] arrc = new int[] {1, 2, 3};
System.out.println(new ArrayList(Arrays.asList(arrc)));

prints address, but i desire to use toString as in ArrayList.

Is it possible ?

+6  A: 

Try:

import java.util.Arrays;

// ...

int[] arrc = new int[] {1, 2, 3};
System.out.println(Arrays.toString(arrc));

Note that the asList(...) does not take an array of primitive int's but takes Objects instead, that's why you see an address-like String appear.

So, doing:

int[] array = {1, 2, 3};
List list = Arrays.asList(array);

results in the same as doing:

int[] array = {1, 2, 3};
List list = new ArrayList();
list.add(array);

Both result in a List that has one element in it: an array of primitive ints.

(only that Arrays.asList(...) returns a List that cannot be modified...)

Bart Kiers
Re: asList. That's bad. So what happens is that a List with a single element gets created, that element being an int[]. Auto(un-)boxing fail...
Thilo
+1  A: 

use Arrays.toString(arrc)

True Soft
+2  A: 

If you just want to print the array:

Arrays.toString( arrc )

If you want to turn an int[] into a List, Arrays.asList does not work, unfortunately (it only works with Object[]):

List<Integer> list = new ArrayList<Integer>(arrc.length);
for (int a: arrc)
   list.add(a);

System.out.println(list); // prints nicely now
Thilo
@Thilo, note that since Java 1.5, `asList(...)` takes a *vararg* of Objects, not an array of Objects. So you can also provide a single Object, or even something like: `asList(1, 2, 3)` (the int's get boxed to `Integer`s!)
Bart Kiers
Yes, but he already has an int[]. unlikely that the numbers are hard-coded in the source.
Thilo
@Thilo, true, but since you said "it only works with `Object[]`" I wanted to let you know it was `Object...` instead of `Object[]`.
Bart Kiers
+1  A: 

Use Apache Commons Lang as your main library after SDK

System.out.println("An object: " + ReflectionToStringBuilder.toString(anObject));
peperg
I don't see the benefit of using a 3rd party library for such an easy task. If blackliteon already has that Apache Commons Jar in his classpath, sure, but adding it just for creating a printable String of an array, which is easily done by the Arrays class, is IMO overkill.
Bart Kiers
It might be worth having a general-purpose 3rd-party library like commons-collections or guava on your classpath anyway, because in most non-trivial projects it will come in useful for other tasks too.
finnw
A: 

using Dollar should be simple:

int[] ary = { 1, 2, 3, 4};
String string = $(ary).toString(); // wrapping Arrays.toString()

alternatively you can convert the int array to List then use the toString method :

List<Integer> list = $(ary).toList(); 

please note that list is an ArrayList: you can even specify which concrete class should be used simply passing a List implementation (it will work with any List implementation with a no-args constructor):

List<Integer> linkedList = $(ary).toList(LinkedList.class);
dfa
A: 

Using Ints.asList from Guava:

import java.util.List;
import com.google.common.primitives.Ints;

// ...

int[] arrc = {1, 2, 3, 4};
List<Integer> list = Ints.asList(arrc);
System.out.println(list);
// Output: "[1, 2, 3, 4]"
finnw