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.
打印地址,但我希望在ArrayList中使用toString。
Is it possible ?
可能吗 ?
6 个解决方案
#1
7
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 Object
s instead, that's why you see an address-like String appear.
请注意,asList(...)不接受原始int的数组,而是接受Object,这就是为什么你会看到类似地址的String出现的原因。
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 int
s.
两者都导致List中包含一个元素:一个原始int数组。
(only that Arrays.asList(...)
returns a List that cannot be modified...)
(只有Arrays.asList(...)返回一个无法修改的List ...)
#2
2
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[]
):
如果你想把一个int []变成一个List,不幸的是Arrays.asList不起作用(它只适用于Object []):
List<Integer> list = new ArrayList<Integer>(arrc.length);
for (int a: arrc)
list.add(a);
System.out.println(list); // prints nicely now
#4
1
Use Apache Commons Lang as your main library after SDK
在SDK之后使用Apache Commons Lang作为主库
System.out.println("An object: " + ReflectionToStringBuilder.toString(anObject));
#5
0
using Dollar should be simple:
使用Dollar应该很简单:
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 :
或者你可以将int数组转换为List然后使用toString方法:
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是一个ArrayList:您甚至可以指定应该使用哪个具体类,只需传递List实现(它将与任何带有no-args构造函数的List实现一起使用):
List<Integer> linkedList = $(ary).toList(LinkedList.class);
#6
0
Using Ints.asList
from Guava:
使用Guava的Ints.asList:
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]"
#1
7
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 Object
s instead, that's why you see an address-like String appear.
请注意,asList(...)不接受原始int的数组,而是接受Object,这就是为什么你会看到类似地址的String出现的原因。
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 int
s.
两者都导致List中包含一个元素:一个原始int数组。
(only that Arrays.asList(...)
returns a List that cannot be modified...)
(只有Arrays.asList(...)返回一个无法修改的List ...)
#2
2
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[]
):
如果你想把一个int []变成一个List,不幸的是Arrays.asList不起作用(它只适用于Object []):
List<Integer> list = new ArrayList<Integer>(arrc.length);
for (int a: arrc)
list.add(a);
System.out.println(list); // prints nicely now
#3
#4
1
Use Apache Commons Lang as your main library after SDK
在SDK之后使用Apache Commons Lang作为主库
System.out.println("An object: " + ReflectionToStringBuilder.toString(anObject));
#5
0
using Dollar should be simple:
使用Dollar应该很简单:
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 :
或者你可以将int数组转换为List然后使用toString方法:
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是一个ArrayList:您甚至可以指定应该使用哪个具体类,只需传递List实现(它将与任何带有no-args构造函数的List实现一起使用):
List<Integer> linkedList = $(ary).toList(LinkedList.class);
#6
0
Using Ints.asList
from Guava:
使用Guava的Ints.asList:
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]"