简体   繁体   English

ArrayList元素拒绝删除

[英]ArrayList elements refuse to be removed

So, I'm making a random Hiragana name generator (don't ask why, okay?) and I've ran into a bit of a problem. 因此,我正在制作一个随机的平假名名称生成器(不要问为什么,好吗?),我遇到了一个问题。 The random name generator works fine for the most part but sometimes it generates long strings of repeated consonants for some reason. 随机名称生成器在大多数情况下都能正常工作,但有时由于某种原因会生成一长串重复的辅音。 So instead of trying to address the problem directly like any normal programmer would, I've decided to try and scan through the ArrayList and delete repeated characters after the random generation: 因此,我决定尝试遍历ArrayList并在随机生成后删除重复的字符,而不是像普通程序员那样直接解决该问题:

ArrayList<String> name = new ArrayList<String>(); 
Iterator <String> it   = name.iterator();  
...      // insert random generation here                   
for (h = 0; h < s; h++) { // s is the length of the ArrayList
  ...    
  String curInd = name.get(h);
  String nextInd = name.get(h+1);
  if (curInd.equals(nextInd)) { // NOT 
    name.remove(h);             // WORKING
    s--;                        // :(
  }
}

String previousName = "";
while (it.hasNext()) {
String currentName = it.next();
if (currentName.equals(previousName)) {
    it.remove();
}
previousName = currentName;
}

This does not work. 这是行不通的。 I'm not getting an error or anything, it just won't delete the repeated characters (or rather repeated Strings, since I made each character a String.) What could be the problem? 我没有收到错误或任何提示,它只是不会删除重复的字符(或者重复的字符串,因为我将每个字符都设为字符串)。这可能是什么问题?

You are changing the indexes as soon as you remove an item. 删除项目后,您将立即更改索引。 Try using the Iterator.remove() function like the following: 尝试使用Iterator.remove()函数,如下所示:

Iterator<String> it = name.iterator();
String previousName = "";

while (it.hasNext()) {
    String currentName = it.next();
    if (currentName.equals(previousName)) {
        it.remove();
    }
    previousName = currentName;
}

Alternatively you can remove all the duplicates with the following one-liner: 另外,您也可以使用以下一栏删除所有重复项:

names = new ArrayList<String>(new LinkedHashSet<String>(names));

Or even better, use LinkedHashSet or HashSet instead of ArrayList from the very start if you don't want any duplicates. 甚至更好的是,如果您不希望有任何重复项,请从一开始就使用LinkedHashSetHashSet而不是ArrayList

您应该使用Iterator.remove以便在遍历列表时删除元素。

Index needs to be less than length of List . 索引必须小于List length

 String nextInd = name.get(h+1);

Above statement will throw IndexOutOfBoundsException . 上面的语句将抛出IndexOutOfBoundsException

Use HashSet, it automatically removes repeated elements, but sorts elements in alphabetical order. 使用HashSet,它会自动删除重复的元素,但会按字母顺序对元素进行排序。

For Arraylist, Try to use this. 对于Arraylist,请尝试使用它。 This may help. 这可能会有所帮助。

              int size=headlines.size();
     for (int i = 0; i < size - 1; i++) {
            // start from the next item after strings[i]
            // since the ones before are checked
            for (int j = i + 1; j < size; j++) {
                // no need for if ( i == j ) here
                if (!headlines.get(j).equals(headlines.get(i)))
                    continue;

                headlines.remove(j);
                // decrease j because the array got re-indexed
                j--;
                // decrease the size of the array
                size--;
            } // for j
        } // for i

You could make use of a Set of some kind to automatically remove duplicate elements, for example... 您可以利用某种Set自动删除重复的元素,例如...

ArrayList<String> name = new ArrayList<String>();
name.add("A");
name.add("A");
name.add("B");
name.add("B");
name.add("B");
name.add("C");
name.add("C");
name.add("C");
System.out.println(name);
Set<String> set = new TreeSet<String>();
set.addAll(name);
System.out.println(set);

This will, of course, remove ALL duplicates, not just those that appear next to each other... 当然,这将删除所有重复项,而不仅仅是彼此相邻出现的重复项...

For example... 例如...

[A, A, B, B, B, C, C, C]
[A, B, C]

Or... 要么...

[A, B, C, B, C, B, C, A]
[A, B, C]

So it might not meet your immediate needs... 因此它可能无法满足您的即时需求...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM