3

I have an ArrayList that contains a number of Strings, I want to be able to iterate through the ArrayLists contents searching for a string containing a semicolon. When the semicolon is found I then want to delete all of the Strings including and after the semicolon string.

So;

this, is, an, arra;ylist, string

Would become:

this, is, an

I feel like this is a very simple thing to do but for some reason (probably tiredness) I can't figure out how to do it.

Here's my code so far

public String[] removeComments(String[] lineComponents)
    {
        ArrayList<String> list = new ArrayList<String>(Arrays.asList(lineComponents));

        int index = 0;
        int listLength = list.size();
        for(String str : list)
        {
            if(str.contains(";"))
            {

            }
            index++;
        }
        return lineComponents;
    }
  • Possible duplicate [What is the best way to filter a Java Collection?](https://stackoverflow.com/questions/122105/what-is-the-best-way-to-filter-a-java-collection) (the accepted answer contains and example of `Collection#removeIf`) – MadProgrammer Feb 07 '18 at 23:41
  • This would work if all of the elements contained a semicolon. As it stands only one string in the arraylist will contain a semicolon. I want to remove that string and all the strings after it. –  Feb 07 '18 at 23:45
  • Then, instead, I'd find the index of the first matching element, then use `ArrayList#subList` to create a sublist of the all the elements from the specified `index` to the end of the `List` and the use `ArrayList#removeAll` – MadProgrammer Feb 07 '18 at 23:47
  • Possible duplicate of [What is the best way to filter a Java Collection?](https://stackoverflow.com/questions/122105/what-is-the-best-way-to-filter-a-java-collection) – Bishoy Kamel Feb 08 '18 at 00:08

3 Answers3

3

This becomes trivial with Java 9:

public String[] removeComments(String[] lineComponents) {
    return Arrays.stream(lineComponents)
                 .takeWhile(s -> !s.contains(";"))
                 .toArray(String[]::new);
}

We simply form a Stream<String> from your String[] lineComponents and take elements until we find a semicolon. It automatically excludes the element with the semicolon and everything after it. Finally, we collect it to a String[].

Jacob G.
  • 28,856
  • 5
  • 62
  • 116
2

First of all I think you are confusing arrays and arraylists. String[] is an array of strings while ArrayList<String> is an arraylist of strings. Take into account that those are not the same and you should read Array and ArrayList documentation if needed.

Then, to solve your problem following the ArrayList approach you can go as follows. Probably it's not the optimum way to do it but it will work.

public List<String> removeComments(List<String> lineComponents, CharSequence finding)
    {
        ArrayList<String> aux = new ArrayList<String>();

        for(String str : lineComponents)
        {
            if(str.contains(finding))
                break;
            else
                aux.add(str);
        }
        return aux;
    }
Drubio
  • 1,157
  • 3
  • 13
  • 30
0

This example is just for performance and bringing back my old favorite arraycopy:

public String[] removeComments(String[] lineComponents) {
    int index = -1;

    for (int i = 0; i < lineComponents.length; i++) {
        if ( lineComponents[i].contains(";") ) {
            index = i;
            break;
        }
    }

    if (index == -1) return lineComponents;

    return Arrays.copyOf(lineComponents, index);
}
tsolakp
  • 5,858
  • 1
  • 22
  • 28