同志们,关于遍历Vector的问题,很疑惑

wtw11897 2010-10-07 10:21:43
举例如下

Random rnd = new Random();
Form form = new Form("form");
Vector vector = new Vector();

for(int i=0;i<10;i++)
vector.addElement(form);

for(int j=0;j<vector.size();j++)
{
int k= rnd.nextInt(vector.size());
vector.removeElementAt(k);
}

如上代码
问题很严重 具体说不清楚
大家试试看
...全文
266 18 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
18 条回复
切换为时间正序
请发表友善的回复…
发表回复
wuxiuxiang 2010-10-22
  • 打赏
  • 举报
回复
[Quote=引用 9 楼 kf156 的回复:]
Java code

for(int j=0;j<vector.size();j++)
{
int k= rnd.nextInt(vector.size());
vector.removeElementAt(k);
}

改成

while(vector.size()>0){
int k= rnd.nextInt(vector.size());
vector.rem……
[/Quote]
up
车把式 2010-10-22
  • 打赏
  • 举报
回复
[Quote=引用楼主 wtw11897 的回复:]
举例如下

Random rnd = new Random();
Form form = new Form("form");
Vector vector = new Vector();

for(int i=0;i<10;i++)
vector.addElement(form);

for(int j=0;j<vector.size();j++)
{
int k= rnd……
[/Quote]
改成j--

Random rnd = new Random();
Test form=new Test();
Vector vector = new Vector();

for(int i=0;i<10;i++)
vector.addElement(form);
//int counts=0;
for(int j=0;j<vector.size();j++)
{

int k= rnd.nextInt(vector.size());
vector.removeElementAt(k);
//counts++;
//System.out.println("counts+="+counts);
j--;
}
softice_ 2010-10-09
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 magong 的回复:]
引用 12 楼 softice_ 的回复:
这样改不行,没等循环完,Vector就空了

真的?理由?
[/Quote]
是我分析错了,不好意思!呵呵
wtw11897 2010-10-09
  • 打赏
  • 举报
回复
这个其实是敌人的飞机的一个方法
我用的Vector存储的
因为敌人的消亡是随机的
而我想在每次循环的时候遍历到每个敌人。。。。
wtw11897 2010-10-09
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 magong 的回复:]
引用楼主 wtw11897 的回复:
for(int j=0;j<vector.size();j++)
{
int k= rnd.nextInt(vector.size());
vector.removeElementAt(k);
}

也可改成

Java code

int size = vector.size();
for(int j=0;j<size;j++)
{……
[/Quote]

呃 。。
是我没说清楚
在此段代码执行的前面有vector.addElement(form);
而且也是随机加入的
而这些代码都是在run()方法内的。。。
magong 2010-10-08
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 softice_ 的回复:]
这样改不行,没等循环完,Vector就空了
[/Quote]
真的?理由?
softice_ 2010-10-08
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 magong 的回复:]
引用楼主 wtw11897 的回复:
for(int j=0;j<vector.size();j++)
{
int k= rnd.nextInt(vector.size());
vector.removeElementAt(k);
}

也可改成

Java code

int size = vector.size();
for(int j=0;j<size;j++)
{……
[/Quote]
这样改不行,没等循环完,Vector就空了
magong 2010-10-08
  • 打赏
  • 举报
回复
[Quote=引用楼主 wtw11897 的回复:]
for(int j=0;j<vector.size();j++)
{
int k= rnd.nextInt(vector.size());
vector.removeElementAt(k);
}
[/Quote]
也可改成

int size = vector.size();
for(int j=0;j<size;j++)
{
int k= rnd.nextInt(vector.size());
vector.removeElementAt(k);
}
kf156 2010-10-08
  • 打赏
  • 举报
回复

for(int j=0;j<vector.size();j++)
{
int k= rnd.nextInt(vector.size());
vector.removeElementAt(k);
}

改成

while(vector.size()>0){
int k= rnd.nextInt(vector.size());
vector.removeElementAt(k);
}

bastengao 2010-10-08
  • 打赏
  • 举报
回复
循环的次数是 verctor.size() / 2
cqfantasy 2010-10-08
  • 打赏
  • 举报
回复
for(int j=0;j<vector.size();j++)


这个循环的次数并不是期望的10次, vector.size()一直在递减
cqfantasy 2010-10-08
  • 打赏
  • 举报
回复
不能回复了?
cqfantasy 2010-10-08
  • 打赏
  • 举报
回复
没有试,
但是至少for(int j=0;j<vector.size();j++)循环条件是有问题的,每次都--1。
实际循环次数并不是10次。
凡员外 2010-10-08
  • 打赏
  • 举报
回复
你在for循环里new form,再添加进容器,就不会出错了
楼主面向对象的思想还不是很成熟
凡员外 2010-10-08
  • 打赏
  • 举报
回复
你在for循环里new form,再添加进容器,就不会出错了
楼主面向对象的思想还不是很成熟
杨贝 2010-10-08
  • 打赏
  • 举报
回复
for(int i=0;i<10;i++)
vector.addElement(form);
?????????
疑惑
wtw11897 2010-10-07
  • 打赏
  • 举报
回复
晕 广告真多
自己顶起来
希望更多的人看到。
wtw11897 2010-10-07
  • 打赏
  • 举报
回复
自己顶~~~~

13,097

社区成员

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

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