eclipse关于ArrayList的问题

ggshelley 2013-02-01 10:38:33
想请问一下,ArrayList跟int数组怎么转化啊?网上看了一些例如:
ArrayList List = new ArrayList();
List.add(1);
List.add(2);
List.add(3);

Integer[] ss = (Integer[])List.toArray();
System.out.println(ss.length);
为什么运行还是出现错误!
转化过来的ss数组能像正常int数组一样使用吗?
...全文
403 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Candylibin 2013-02-03
  • 打赏
  • 举报
回复
import java.util.*;

class  Testa
{
	public static void main(String[] args) 
	{
		List<Integer> al = new ArrayList<Integer>();
		al.add(1);
		al.add(2);
		al.add(3);
		
		Integer[] arr = al.toArray(new Integer[al.size()]);

		System.out.println(Arrays.toString(arr));
		
		System.out.println("长度="+arr.length);
	}
}
简易人 2013-02-02
  • 打赏
  • 举报
回复

 public static void main(String[]args){
    	 ArrayList array=new ArrayList();
    	 array.add(1);
    	 array.add(5);
    	 array.add(3);
    	Integer[] in=new Integer[array.size()];
    	Object[] obj=array.toArray();
    	for(int i=0;i<in.length;i++){
    		in[i]=(Integer)obj[i];
			System.out.println(in[i]);
		}
     }


abc41106 2013-02-02
  • 打赏
  • 举报
回复
引用 3 楼 ggshelley 的回复:
引用 1 楼 duxingzhe0311 的回复:手写的,木有测,大概是这意思吧!toArray可以接收一个泛型数组。。。 Java code?12Integer[] ss = new Integer[list.size()];ss = List.toArray(ss); 还得转化一下:ss = (Integer[]) List.toArray(ss); 那现在……
转换之后ss是Integer类型的数组,不是int类型的数组。但是不影响一般操作。
hechuanchuan 2013-02-01
  • 打赏
  • 举报
回复

Integer[] ss = (Integer[]) list.toArray(new Integer[list.size()]);
ggshelley 2013-02-01
  • 打赏
  • 举报
回复
引用 1 楼 duxingzhe0311 的回复:
手写的,木有测,大概是这意思吧!toArray可以接收一个泛型数组。。。 Java code?12Integer[] ss = new Integer[list.size()];ss = List.toArray(ss);
还得转化一下:ss = (Integer[]) List.toArray(ss); 那现在这个ss是什么类型的啊?
rockets311 2013-02-01
  • 打赏
  • 举报
回复
import java.util.*;

class Test
{
	public static void main(String[] args)
	{
		List<Integer> list = new ArrayList<Integer>(); 
		list.add(1); 
		list.add(2); 
		list.add(3);
		 
		Integer[] ss = new Integer[list.size()];
		ss = list.toArray(ss); 
		System.out.println(Arrays.toString(ss));
	}
}
rockets311 2013-02-01
  • 打赏
  • 举报
回复
手写的,木有测,大概是这意思吧!toArray可以接收一个泛型数组。。。
Integer[] ss = new Integer[list.size()];
ss = List.toArray(ss); 

58,453

社区成员

发帖
与我相关
我的任务
社区描述
Java Eclipse
社区管理员
  • Eclipse
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧