ArrayList

BlackSky_Sun 2008-05-23 11:06:39
我用 Random产生了一百个随机数。
然后我用ArrayList保存。
假如产生了 1,3,24,53,43....................
我怎样才能让ArrayList 按照下面格式保存 ('1','3','24','53','43')

当我用Iterator 迭代出来的时候又变成(1,3,24,53,43)这种样式...........

请高手帮忙。。。急。。。。。。。。。。。。
...全文
129 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
BlackSky_Sun 2008-05-23
  • 打赏
  • 举报
回复
不是。。
我的目的是想做一个随机出题并且对应该题而得出答案。。

题目表 examSubject(id主键) 答案表examAnswer(tid外键 )

产生的随机数 要和id字段相等时 就读取数据库中的信息保存到JavaBean然后在封装到ArrayList中
但是当他相等时就查询数据库。平凡的炒作内存,cpu使用很多。运行速度太慢了
有没有更好的设计思路.....................
看下我的源代码就知道我的意思了 。。。

TomyGuan 2008-05-23
  • 打赏
  • 举报
回复
你每次都bb=new ArrayList();

是不是要把bb=new ArrayList(); 放到while的上面上去呢???
spaceko2006 2008-05-23
  • 打赏
  • 举报
回复
你是说打印出来又变成整数了吗,
不明白你是怎么看的it。
BlackSky_Sun 2008-05-23
  • 打赏
  • 举报
回复
我 现在正在做一个随机试卷的问题
这是我的源代码...有两个表。问题和答案表 当每次产生随机数的时候就查询数据库那样太耗内存了。但是暂时又向不到更好的办法请各位帮忙谢谢又什么好的办法没?
public boolean Exists(List<Integer> array,int number){ //判读这个号码是否存在
for(int i=0;i<array.size();i++){
if(array.get(i)==number){
return true;
}
}
return false;
}
public ArrayList getMyList(){ //随机数
int array [] =new int [20];
connect=con.getCon();
String sql="select a.examContent,a.examId,b.examAnswer,b.examScore,b.examtid from examsubject as a inner join examanswer as b on a.examid=b.examtid and a.examid=?";
List<Integer> arrayList = new ArrayList<Integer>();
bb=new ArrayList();
Random rd=new Random();
for(int i=0;i<100;i++){ //该循环执行的次数true
randomNum=(int)rd.nextInt(5);//产生5个随机数
if(Exists(arrayList,randomNum)){
}else{
try {
PreparedStatement pst=connect.prepareStatement(sql);
pst.setInt(1, randomNum);
rs=pst.executeQuery();
while(rs.next()){
bb=new ArrayList();
myBean=new AnswerBean();
arrayList.add(randomNum);
myBean.setExamID(rs.getInt("examID"));
myBean.setExamTid(rs.getInt("examTid"));
myBean.setExamContent(rs.getString("examContent"));
myBean.setExamAnswer(rs.getString("examAnswer"));
myBean.setExamScore(rs.getInt("examScore"));
bb.add(myBean);
}
return bb;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
return null;
}
ilrxx 2008-05-23
  • 打赏
  • 举报
回复
list.add("'1'");
i_saw_you 2008-05-23
  • 打赏
  • 举报
回复
崩溃,放进去字符,取出来整数,你该不是想做个类型转换机吧
类型转换机是很好做的public class chage{
String s;
pubilc String getS(){
if(s!=null){
return s.valureof(s);//此处要捕获铸型错误异常
}else{
return s
}
}
pubilc viod SetS(String s){
this.s=s;
}
}
大概就是这个思路,代码没有调试
fuyou001 2008-05-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 imA 的回复:]
自己手动编码进行字符串的拼接。

但是我不明白你为什么要这样做?
[/Quote]
burningice44 2008-05-23
  • 打赏
  • 举报
回复
有些时候可以想些更简单直接的方式解决
imA 2008-05-23
  • 打赏
  • 举报
回复
自己手动编码进行字符串的拼接。

但是我不明白你为什么要这样做?
berlou 2008-05-23
  • 打赏
  • 举报
回复
自己扩展一个ArrayList类, 覆盖add方法和get方法应该可以。
也许需要覆盖iterator方法。
i_saw_you 2008-05-23
  • 打赏
  • 举报
回复
// bb=new ArrayList(); 这一行是手误,不注视的.
i_saw_you 2008-05-23
  • 打赏
  • 举报
回复
public boolean Exists(List <Integer> array,int number){ //判读这个号码是否存在
for(int i=0;i <array.size();i++){
if(array.get(i)==number){
return true;
}
}
return false;
}
public ArrayList getMyList(){ //随机数
//int array [] =new int [20];
connect=con.getCon();
String sql="select a.examContent,a.examId,b.examAnswer,b.examScore,b.examtid from examsubject as a inner join examanswer as b on a.examid=b.examtid and a.examid in (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";
// List <Integer> arrayList = new ArrayList <Integer>();
// bb=new ArrayList();
Random rd=new Random();
// if(Exists(arrayList,randomNum)){
// }else{
try {
for(int i=0;i <5;i++){
randomNum=(int)rd.nextInt(5);
PreparedStatement pst=connect.prepareStatement(sql);
pst.setInt(i, randomNum);
}

rs=pst.executeQuery();
while(rs.next()){
bb=new ArrayList();
myBean=new AnswerBean();
arrayList.add(randomNum);
myBean.setExamID(rs.getInt("examID"));
myBean.setExamTid(rs.getInt("examTid"));
myBean.setExamContent(rs.getString("examContent"));
myBean.setExamAnswer(rs.getString("examAnswer"));
myBean.setExamScore(rs.getInt("examScore"));
bb.add(myBean);
}
return bb;
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
return null;
}
}
}
return null;
}


我修改你的 看看
mike123hl 2008-05-23
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 imA 的回复:]
自己手动编码进行字符串的拼接。

但是我不明白你为什么要这样做?
[/Quote]

81,092

社区成员

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

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