在Java中修改ArrayList
我想搜索ArrayList并删除所有相同的条目 例如,如果我的列表是:苹果、橘子、香蕉、梨、桃、橘子, 然后删除“橙色”(两种情况) 我天真地尝试:在Java中修改ArrayList,java,arraylist,Java,Arraylist,我想搜索ArrayList并删除所有相同的条目 例如,如果我的列表是:苹果、橘子、香蕉、梨、桃、橘子, 然后删除“橙色”(两种情况) 我天真地尝试: for(String word : userlist){ for(String otherword : userlist){ ... } } 在那里我写了how.remove(lastIndexOf(userword))如果它等于word和它们的索引是不同的 这导致了一个接一个的异常,我很快意识到我在遍历一个列表时正在操作一个列表,这会使它出
for(String word : userlist){
for(String otherword : userlist){
...
}
}
在那里我写了how.remove(lastIndexOf(userword))如果它等于word和它们的索引是不同的
这导致了一个接一个的异常,我很快意识到我在遍历一个列表时正在操作一个列表,这会使它出错
所以决定复制一份名单
ArrayList<String> copylist = userlist;
for(String word : copylist){
for(String otherword : copylist){
if(word.equalsIgnoreCase(otherword)
&& copylist.lastIndexOf(word)!=copylist.lastIndexOf(otherword)){
userlist.remove(userlist.lastIndexOf(word));
userlist.remove(userlist.lastIndexOf(otherword));
}
}
}
arraylistcopylist=userlist;
for(字符串字:复制列表){
for(字符串其他词:copylist){
if(word.equalsIgnoreCase(otherword)
&©list.lastIndexOf(word)!=copylist.lastIndexOf(otherword)){
remove(userlist.lastIndexOf(word));
移除(userlist.lastIndexOf(otherword));
}
}
}
所以我试过这个,它也有类似的问题。特别是ConcurrentModificationException。经过调整后,我无法得到,在我的脑海中应该是一个相当简单的过程,在Java中工作。请帮助。您目前根本没有制作该列表的副本。您正在声明一个引用同一列表的新变量。要制作列表的副本,请使用:
ArrayList<String> copyList = new ArrayList<String>(userList);
arraylistcopylist=新的ArrayList(userList);
但是,我建议采用不同的方法:
ArrayList<String> wordsToRemove = new ArrayList<String>();
Set<String> seenWords = new HashSet<String>();
for (String word : userList)
{
if (!seenWords.add(word))
{
wordsToRemove.add(word);
}
}
for (String word : wordsToRemove)
{
// Keep removing it until it doesn't exist any more
while (userList.remove(word)) {}
}
ArrayList wordsToRemove=new ArrayList();
Set seenWords=new HashSet();
for(字符串字:userList)
{
如果(!seenWords.add(word))
{
wordsToRemove.add(word);
}
}
for(字符串字:wordsToRemove)
{
//继续移除它,直到它不再存在
while(userList.remove(word)){}
}
然而,这并没有忽略这个案例。要做到这一点,你需要更聪明一点:
Set<String> wordsToRemove = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
Set<String> seenWords = new TreeSet<String>(String.CASE_INSENSITIVE_ORDER);
for (String word : userList)
{
if (!seenWords.add(word))
{
wordsToRemove.add(word);
}
}
// Now we know the words we don't want, step through the list again and
// remove them (case-insensitively, as wordsToRemove is case-insensitive)
for (Iterator<String> iterator = userList.iterator(); it.hasNext() ;)
{
if (wordsToRemove.contains(word))
{
iterator.remove();
}
}
Set wordsToRemove=new TreeSet(String.CASE不区分大小写\u顺序);
Set seenWords=new TreeSet(String.CASE不区分大小写\u顺序);
for(字符串字:userList)
{
如果(!seenWords.add(word))
{
wordsToRemove.add(word);
}
}
//现在我们知道了我们不想要的单词,请一次又一次地浏览列表
//删除它们(不区分大小写,因为wordsToRemove不区分大小写)
for(Iterator Iterator=userList.Iterator();it.hasNext();)
{
if(wordsToRemove.contains(word))
{
iterator.remove();
}
}
arraylistcopylist=userlist;
此行将引用userlist分配给copylist,并且不会创建新的arraylist。它指向同一个arraylist。
要创建一个新列表,简单的方法是创建一个全新的列表,并在签入新列表中是否已存在项后,继续在该新列表中添加项
ArrayList<String> newList = new ArrayList<String> ();
foreach(String item in userList) {
if(newList.contains(item)==false)
{
newList.add(item);
}
}
arraylistnewlist=newarraylist();
foreach(用户列表中的字符串项){
if(newList.contains(item)==false)
{
新建列表。添加(项);
}
}
这是众所周知的问题-您无法修改迭代的容器。
解决的技巧是使用迭代器的方法remove:
Iterator<String> tblIter = array.iterator();
while (tblIter.hasNext()) {
String entry = tblIter.next();
if( entry.equals(.....) )
{
...
tblIter.remove();
}
}
Iterator tbl=array.Iterator();
while(tbl.hasNext()){
String entry=tbl.next();
如果(条目等于(…))
{
...
t升。移除();
}
}
如果您希望在集合的迭代过程中删除元素,则必须使用迭代器和迭代器。删除()
但我想建议你们更简单的解决办法:把你们的清单放在一组中。它会自动删除重复项:
List<String> mylist = new ArrayList<String>();
// some initialization: there are duplicates
Set<String> myset = new HashSet<String>(mylist); // no duplicates here.
List mylist=new ArrayList();
//一些初始化:存在重复项
Set myset=newhashset(mylist);//这里没有重复的。
您可以继续使用set,因为它是一个集合,您可以对它进行迭代。但如果需要随机访问,请再次创建列表:
newlist = new ArrayList<String>(myset);
newlist=newarraylist(myset);
由于散列,集合中的元素顺序将是“随机”的。如果您希望保留原始顺序,请使用LinkedHashSet而不是HashSet。如果您可以控制数据的添加方式,您可以创建自己的“add”方法,如下所示:
add(element)
{
if arrayList.contains(element)
arrayList.remove(arrayList.indexOf(element))
else
arrayList.add(element)
}
另一方面,如果您无法控制如何/何时添加数据,则可以执行循环,并在其中使用与上述类似的逻辑。查看适当的方法。import java.util.ArrayList;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Test {
public static void main(String[] args) {
List<String> a = new ArrayList<String>();
a.add("apple");
a.add("orange");
a.add("banana");
a.add("pear");
a.add("peach");
a.add("orange");
System.out.println(a);
System.out.println(getSingleWordList(a));
}
private static List<String> getSingleWordList(List<String> list)
{
Set<String> uniques = new HashSet<String>();
Set<String> dups = new HashSet<String>();
for (String a : list)
if (!uniques.add(a))
dups.add(a);
uniques.removeAll(dups);
return new ArrayList<String>(uniques);
}
}
导入java.util.HashSet;
导入java.util.List;
导入java.util.Set;
公开课考试{
公共静态void main(字符串[]args){
列表a=新的ArrayList();
a、 添加(“苹果”);
a、 添加(“橙色”);
a、 添加(“香蕉”);
a、 加上(“梨”);
a、 添加(“桃子”);
a、 添加(“橙色”);
系统输出打印项次(a);
System.out.println(getSingleWordList(a));
}
私有静态列表getSingleWordList(列表列表)
{
Set uniques=new HashSet();
Set dups=new HashSet();
for(字符串a:列表)
如果(!uniques.add(a))
重复。添加(a);
uniques.removeAll(dups);
返回新的ArrayList(uniques);
}
}
输出
Input=[苹果、橘子、香蕉、梨、桃、橘子]
<代码>输出= [梨,苹果,香蕉,桃子] < /代码>
< p>如果没有重复元素的集合,请考虑一个集合是否比列表更合适。这将以一个没有重复的列表结束,但它仍然包含一个“橙色”实例,例如“代码>前缀(用户列表中的字符串项))。这不是Java。你是说(字符串项:userList)@S.P.Floyd,对不起,foreach。我一年前离开了爪哇。。现在在C#中工作,所以习惯于foreach..这可能就是Jon Skeet没有注意到的原因:-)这将以一个“orange”实例结束,而不是同时删除这两个实例。这个想法是删除任何复制值的所有副本。据我所知,他希望删除重复的两个实例。此外,您可以使用LinkedHashSet来保持顺序并重用现有列表(<import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
public class Test {
public static void main(String[] args) {
List<String> a = new ArrayList<String>();
a.add("apple");
a.add("orange");
a.add("banana");
a.add("pear");
a.add("peach");
a.add("orange");
System.out.println(a);
System.out.println(getSingleWordList(a));
}
private static List<String> getSingleWordList(List<String> list)
{
Set<String> uniques = new HashSet<String>();
Set<String> dups = new HashSet<String>();
for (String a : list)
if (!uniques.add(a))
dups.add(a);
uniques.removeAll(dups);
return new ArrayList<String>(uniques);
}
}