`
53873039oycg
  • 浏览: 824717 次
  • 性别: Icon_minigender_1
社区版块
存档分类
最新评论

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

    博客分类:
  • java
 
阅读更多

       看了博文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

    全文完

     

0
0
分享到:
评论
2 楼 53873039oycg 2014-05-26  
dianqiugg 写道
楼主你在说什么,ArrayUtils.toObject(b)这个做了什么

public static Integer[] toObject(int array[]) {
		if (array == null)
			return null;
		if (array.length == 0)
			return EMPTY_INTEGER_OBJECT_ARRAY;
		Integer result[] = new Integer[array.length];
		for (int i = 0; i < array.length; i++)
			result[i] = new Integer(array[i]);

		return result;
	}
1 楼 dianqiugg 2014-05-26  
楼主你在说什么,ArrayUtils.toObject(b)这个做了什么

相关推荐

Global site tag (gtag.js) - Google Analytics