[简单]Arrays.asList转换int[]

       看了博文http://my.oschina.net/jasonli0102/blog/268988。才注意到:

写道
Arrays.asList() 对基本类型数组转换后list中只有一个元素

     测试例子:

     代码看不懂的请自己调试

package com.huse.listtest;

import java.util.Arrays;
import java.util.List;

import org.apache.commons.lang.ArrayUtils;

public class ArraysAsListTest {
	public static void main(String[] args) {
		int[] b = new int[] { 1, 2, 3, 4, 5 };
		List listA = Arrays.asList(b);
		System.out.println(listA.getClass().getName());
		System.out.println(listA + "   ,size=" + listA.size());
		System.out.println(((int[])listA.get(0))[2]+"  ,size="+((int[])listA.get(0)).length);

		List<Integer> listB = Arrays.asList(ArrayUtils.toObject(b));
		System.out.println(listB.getClass().getName());
		System.out.println(listB + "   ,size=" + listB.size());

		Integer[] a2 = new Integer[] { 1, 2, 3, 4, 5 };
		List<Integer> listC = Arrays.asList(a2);
		System.out.println(listC.getClass().getName());
		System.out.println(listC + "   ,size=" + listC.size());
	}

}

 

   结果:

 

java.util.Arrays$ArrayList
[[I@15db9742]   ,size=1
3  ,size=5
java.util.Arrays$ArrayList
[1, 2, 3, 4, 5]   ,size=5
java.util.Arrays$ArrayList
[1, 2, 3, 4, 5]   ,size=5

   解释在:

 

   http://stackoverflow.com/questions/1467913/arrays-aslist-not-working-as-it-should

 

    http://stackoverflow.com/questions/12020886/how-arrays-aslistint-can-return-listint

写道
List<int> is not permitted in Java, when you use a int[] array as parameter for Arrays.asList it will consider it as the single element of a list instead of an array of int

    全文完

     

你可能感兴趣的:(Arrays)