正在加载图片...
public void removeLongStrings( Collection coll, int maxLen)( Iterator it=coll. iterator while(it. hasNextoi String str=(String)it. nexto; if(strength(>maxLen) coll.remove(str;错误的删除操作! public void remove LongStrings( Collection coll, int maxLen) Iterator it=coll. iterator 0: while(it. hasNextO) String str=(String)it. nexto if(strlength(>maxLen) it remove;正确的删除操作!java.util.Iterator Interface • 该接口声明了用于遍历集合的方法: public boolean hasNext() public Object next() public void remove() • 通过调用Collection接口及其子接口的实现类的对 象的iterator()方法可以返回一个该接口的实现类的 对象,使用该对象来遍历访问集合中每个对象 • 在使用Iterator对象遍历集合时,如果要删除集合 中的某个对象,必须使用该接口中remove()方法! public void removeLongStrings(Collection coll, int maxLen){ Iterator it=coll.iterator(); while(it.hasNext()){ String str=(String)it.next(); if(str.length()>maxLen) coll.remove(str); } } 错误的删除操作! public void removeLongStrings(Collection coll, int maxLen){ Iterator it=coll.iterator(); while(it.hasNext()){ String str=(String)it.next(); if(str.length()>maxLen) it.remove(); } } 正确的删除操作!
<<向上翻页向下翻页>>
©2008-现在 cucdc.com 高等教育资讯网 版权所有