「Java言語」Arrays.asListをint[]に変換する方法
Javaコード:
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
参考URL:
http://stackoverflow.com/questions/1467913/arrays-aslist-not-working-as-it-should
http://stackoverflow.com/questions/12020886/how-arrays-aslistint-can-return-listint