如何将Arraylist转换成int数组?

meteornk 2004-01-09 02:59:57
或者将arraylist中的某一个object转换成int类型?
...全文
2956 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
braveheart2000 2004-01-09
  • 打赏
  • 举报
回复
import java.util.*;
public class test {
public static void main(String args[]) {
ArrayList tmpList = new ArrayList();
tmpList.add(new Integer(1));
tmpList.add(new Integer(2));
tmpList.add(new Integer(3));
Integer tmpInteger[] = new Integer[tmpList.size()];
int tmpInt[] = new int[tmpList.size()];
tmpList.toArray(tmpInteger);
for(int i = 0; i < tmpInteger.length;i++) {
tmpInt[i] = tmpInteger[i].intValue();
System.out.println(tmpInt[i]);
}
}
}
过客猫2022 2004-01-09
  • 打赏
  • 举报
回复
fast_time(fast_time) 你的那个是错的,不能那样转换!toArray只是得到Object[],而不是int[].
bookhome(bookhome) 我试过,你的这种方法可以的,不过要改一下
int ret[]=new int[arrayList.size()];
for(int i=0;i<arrayList.size();i++)
{
ret[i]=((Integer)arrayList.get(i)).intValue();
}
其实转过来,没有什么意义!
binny 2004-01-09
  • 打赏
  • 举报
回复
用Integer数组不行吗,必须int数组吗?还得多intValue一次,多麻烦。
sisn 2004-01-09
  • 打赏
  • 举报
回复
ArrayList里必须是Object,int绝对不行...
呵呵.java基础问题,楼上的都要看书
bookhome 2004-01-09
  • 打赏
  • 举报
回复
Iterator iterator = arrayList.iterator();
int[] ret = new int[arrayList.size()];
int i=0;
while(iterator.hasNext()){
ret[i++] = Integer.parseInt((String)iterator.next());
}

呵呵,没有测试过
fast_time 2004-01-09
  • 打赏
  • 举报
回复
int[] array = (int[])ArrayList.toArray(new int[])
meteornk 2004-01-09
  • 打赏
  • 举报
回复
例如,
int(arraylist.get(i))?
好像不行
chlei 2004-01-09
  • 打赏
  • 举报
回复
强制转换不行吗?

62,623

社区成员

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

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