0

I've looked over a few other posts on the topic, but I don't understand Java well enough to understand those questions or their answers. Basically, I'm trying to shrink a String array, removing all values that don't match a given length. For instance, if the String is "pen", "pencil", "note", "paper", "desk", "object", "stuff" and I'm only looking for Strings with length 4, I want to have an array that only has "note" and "desk". I thought that the 'remove()' method could help me, but I must be implementing it wrong. This is what I have:

String[]wordArray = playerWordArray;
    for (int i = 0; i < wordArray.length; i++) {
        if (wordArray[i].length() != length) {
            wordArray.remove();
    }
  • 2
    Arrays don't have `remove` method. If you want to remove - use a list! – Nir Alfasi Nov 02 '17 at 18:56
  • To add to @alfasin, once you've switched to lists, you can't remove an element whilst iterating the list at the same time. Either create another list which will store the elements that satisfy your criteria or use [iterators](https://docs.oracle.com/javase/7/docs/api/java/util/ListIterator.html) – eshirima Nov 02 '17 at 18:59
  • Wait, there's a difference between lists and arrays? 0_0 – Maeve Fitzgerald Nov 02 '17 at 19:37

0 Answers0