Baker1562 Baker1562 - 11 months ago 60
Java Question

Find value in String[] inside List and erase the String[]

List<String[]> data = new ArrayList<>();
data.add(new String[]{"bdc", "house2", "car2"});
data.add(new String[]{"abc", "house", "car"});

I have to questions about it:

  1. How to find for example, what is the value or second argument if I have
    , I want to find the value
    ? (I don't know what is my second argument of course only the first);

  2. How to erase all the
    if I have again for example

Answer Source
  1. To access to a given index of an array, use the square brackets array[index] knowing that index goes from 0 to array.length - 1.
  2. To remove an element of a Collection while iterating over it, you can use iterator.remove().

So your code could look like:

// Flag used to know if it has already been found
boolean found = false;
for (Iterator<String[]> it = data.iterator(); it.hasNext();) {
    String[] values =;
    // Check if the first element of the array is "abc"
    if (values.length > 1 && "abc".equals(values[0])) {
        if (found) {
            // Already found so we remove it
        // Not found yet so we simply print it
        found = true;