程序通不过,我倒霉死了。一个关于Vector的问题

myelan 2002-10-04 05:38:51
我写了一段程序

需要对一个Vector做一个循环,
在循环中,我需要删除一些Vector的元素,

于是,我就想用Iterator接口
Iterator thePoint=D.iterator();

while (thePoint.hasNext())
{
p=(Point)thePoint.next();/////这一行报错
if (isalone(p,D))
{
O.add(p);
D.remove(p);
//thePoint.remove(p);
//CC.add(O);outtofile(CC);
}
else
{
……
}
}
好像是由于其中修改了D的内容,结果编译没有问题,运行报错
java.util.ConcurrentModificationException
at java.util.AbstractList$Itr.checkForComodification(AbstractList.java:444)
at java.util.AbstractList$Itr.next(AbstractList.java:417)
at Cluster.main(Cluster.java:217)
注:217就是上面的注释了的代码


那位好心帮我想想,有没有什么好办法
谢谢
...全文
141 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
DanielYWoo 2002-10-05
  • 打赏
  • 举报
回复
严重推荐Java Tutorial的集合那一部分
讲的比Core Java还好!
DanielYWoo 2002-10-05
  • 打赏
  • 举报
回复
我不是说了么
“遍历”时只能用iterator去remove()
不能插入,你就别老费心神总想在“遍历的时候插入了"
你为什么非要在遍历时插呢?
看看Sun的Java Tutorial的
关于集合访问的安全解释
你就知道为什么了
jym212cn 2002-10-05
  • 打赏
  • 举报
回复
我倒是有个想法,可以遍历时先把你要删除的元素的索引记在另一个VECTOR里,然后再做一遍遍历时来删除那些元素,不知道可行否?
cyrano 2002-10-04
  • 打赏
  • 举报
回复
不清楚可以参考《Java2 核心技术 卷II》中的“数据结构”一章,大概对你会有帮助。
litxiang 2002-10-04
  • 打赏
  • 举报
回复
如果只删除的话用Vector,Iterator就够了。
Iterator.remove删除的是next()刚刚返回的元素,
你可以先判断其是否需要删除。
删除后仍可以继续遍历,
继续删除。
myelan 2002-10-04
  • 打赏
  • 举报
回复
在listIterator上add,remove元素

这样的话
再Iterator上也可以了

但是,因为我要修改的可能不是next()的元素
而且是后面一块的

怎么办?

我已经知道,Iterator的有关操作了

希望大家帮忙
解决我的问题:
在循环中,怎么删除Vector的元素(不是一个一个的,这种可以
.next()
.remove()来删除)
但是要删除很多,不可能先用Iterator来定位的
有什么好办法
.)
myelan 2002-10-04
  • 打赏
  • 举报
回复
看了,不过你给我这个是什么意思呢?

sunspot 2002-10-04
  • 打赏
  • 举报
回复
Iterator接口只有三个方法:
这是Iterator的源代码。
/*
* @(#)Iterator.java 1.16 01/12/03
*
* Copyright 2002 Sun Microsystems, Inc. All rights reserved.
* SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
*/

package java.util;

/**
* An iterator over a collection. Iterator takes the place of Enumeration in
* the Java collections framework. Iterators differ from enumerations in two
* ways: <ul>
* <li> Iterators allow the caller to remove elements from the
* underlying collection during the iteration with well-defined
* semantics.
* <li> Method names have been improved.
* </ul>
*
* @author Josh Bloch
* @version 1.16, 12/03/01
* @see Collection
* @see ListIterator
* @see Enumeration
* @since 1.2
*/
public interface Iterator {
/**
* Returns <tt>true</tt> if the iteration has more elements. (In other
* words, returns <tt>true</tt> if <tt>next</tt> would return an element
* rather than throwing an exception.)
*
* @return <tt>true</tt> if the iterator has more elements.
*/
boolean hasNext();

/**
* Returns the next element in the iteration.
*
* @return the next element in the iteration.
* @exception NoSuchElementException iteration has no more elements.
*/
Object next();

/**
*
* Removes from the underlying collection the last element returned by the
* iterator (optional operation). This method can be called only once per
* call to <tt>next</tt>. The behavior of an iterator is unspecified if
* the underlying collection is modified while the iteration is in
* progress in any way other than by calling this method.
*
* @exception UnsupportedOperationException if the <tt>remove</tt>
* operation is not supported by this Iterator.

* @exception IllegalStateException if the <tt>next</tt> method has not
* yet been called, or the <tt>remove</tt> method has already
* been called after the last call to the <tt>next</tt>
* method.
*/
void remove();
}
myelan 2002-10-04
  • 打赏
  • 举报
回复
谢谢上面的几位,小相,我试试用你说的办法吧
可是,那需要改我很多,如果用vector没有办法解决么????

DanielYWoo(绿色毒汁) 你讲得好透彻的,谢谢

DanielYWoo 2002-10-04
  • 打赏
  • 举报
回复
Iterator遍历过程中,有concurrent保护
这是相对Enumeration最大的改进
你最多只可以用Iterator的remove方法删除,
(这也是SUN推荐的唯一安全的方法)
添加的话,不可以在遍历中添加!
下面这个例子


import java.util.*;
import java.awt.Point;

public class Test1
{
public static void main(String s[])
{
HashSet d = new HashSet();
//下面的添加是对的
d.add(new Point(1,1));
d.add(new Point(2,2));
d.add(new Point(3,3));
d.add(new Point(4,4));
d.add(new Point(5,5));
d.add(new Point(6,6));
d.add(new Point(7,7));

Iterator iterator = d.iterator();
Random rand = new Random();
//随机的删除d内的元素
//下面的删除是对的
System.out.println("Tranversing and random deleting");
while (iterator.hasNext())
{
Point p = (Point)iterator.next();
System.out.println(p);
if (rand.nextInt()%2==1)
iterator.remove();
}
//输出结果
System.out.println("The deletion result:");
iterator = d.iterator();
while (iterator.hasNext())
{
Point p = (Point)iterator.next();
System.out.println(p);
}
//下面的添加是错的
System.out.println("The addition result:");
iterator = d.iterator();
while (iterator.hasNext())
{
Point p = (Point)iterator.next();
d.add(new Point(31,31));
System.out.println(p);
}
}
}
season_fly 2002-10-04
  • 打赏
  • 举报
回复
用stack多好啊
litxiang 2002-10-04
  • 打赏
  • 举报
回复
另外,应该在listIterator上add,remove元素
litxiang 2002-10-04
  • 打赏
  • 举报
回复
Iterator不支持遍历时添加元素。
用ArrayList和ListIterator试试
List list=new ArrayList();
ListIterator iter=list.listIterator();
while(list.hasNext()){
...
}
myelan 2002-10-04
  • 打赏
  • 举报
回复
不会的帮帮忙
UP一下

谢谢

62,634

社区成员

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

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