----如何删除ArrayList中重复的元素-----

Powerliang 2004-08-03 02:09:25
我用下面的方法删除ArrayList中的相同的元素时,会出现异常,我感觉我的remove语句不应该写在程序中的位置,但具体应该如何解决还不时很清楚,请高手指教!


ArrayList arrWords = new ArrayList();
arrWords.add("A");
arrWords.add("B");
arrWords.add("A");
arrWords.add("B");
int len = arrWords.size();
for(int count=0; count<len; count++)
{
for(int index=len-1; index>=0; index--)
{
if(arrWords.get(count).equals(arrWords.get(index)))
{
arrWords.remove(count); //异常
}
}
}
...全文
164 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhang21cnboy 2004-08-03
  • 打赏
  • 举报
回复
好好学习collection
showerXP 2004-08-03
  • 打赏
  • 举报
回复
嘿嘿。用set
scvzhang 2004-08-03
  • 打赏
  • 举报
回复
用一下set
sxyan 2004-08-03
  • 打赏
  • 举报
回复
for (int count = 0; count < len; count++) {
for (int index = count + 1 ; index < len; index++) {
if (arrWords.get(count).equals(arrWords.get(index))) {
arrWords.set(index,"");
}
}
}
for (int index=arrWords.size()-1; index>=0; index--){
if (arrWords.get(index)=="") arrWords.remove(index);
}
sxyan 2004-08-03
  • 打赏
  • 举报
回复
先作个标记吧,这个肯定会有问题。

62,616

社区成员

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

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