如何将ArrayList转换成整型数组
1个回答
展开全部
ArrayListtoArray()list转化Object[]数组何转化其类型数组呢比泛型IntegerArrayList何转化数组呢
public static void main(String[] args) {
// TODO Auto-generated method stub
/*list转化String[]*/
ArrayList stringList = new ArrayList();//泛型String
stringList.add("123");
stringList.add("abc");
stringList.add("2js");
String[] a = new String[stringList.size()];
a = (String[]) stringList.toArray(a);
System.out.println(Arrays.toString(a));
/*list转化Integer[]*/
ArrayList intList = new ArrayList();//泛型Integer
intList.add(123);
intList.add(234);
intList.add(345);
Integer[] b = new Integer[intList.size()];//泛型Integer需要
b = (Integer[])intList.toArray(b); //Integer类作数组基本元素
System.out.println(Arrays.toString(b));//否则输报错
Integer[] c = (Integer[])intList.toArray();//处编译没问题涉及泛型
System.out.println(Arrays.toString(c)); //向转换运行错
System.out.println(c.getClass());
System.out.println(b.getClass());
}
public static void main(String[] args) {
// TODO Auto-generated method stub
/*list转化String[]*/
ArrayList stringList = new ArrayList();//泛型String
stringList.add("123");
stringList.add("abc");
stringList.add("2js");
String[] a = new String[stringList.size()];
a = (String[]) stringList.toArray(a);
System.out.println(Arrays.toString(a));
/*list转化Integer[]*/
ArrayList intList = new ArrayList();//泛型Integer
intList.add(123);
intList.add(234);
intList.add(345);
Integer[] b = new Integer[intList.size()];//泛型Integer需要
b = (Integer[])intList.toArray(b); //Integer类作数组基本元素
System.out.println(Arrays.toString(b));//否则输报错
Integer[] c = (Integer[])intList.toArray();//处编译没问题涉及泛型
System.out.println(Arrays.toString(c)); //向转换运行错
System.out.println(c.getClass());
System.out.println(b.getClass());
}
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询