2

I have an ArrayList of String arrays which is added to like so:

List<String[]> data = new ArrayList<>();
data.add(new String[] {var, lex, valor});

When trying to erase one of the String arrays the output is false for some reason. Here's how I remove it:

data.remove(new String[] {var, lex, valor});

I've also tried removing using position, the method is as follows:

public void eliminarPos(String var, String lex, String valor, Integer ii) {
    boolean uno = data.remove(ii);
    System.out.println(uno);
}

The output for the method above is false. Is there any way to successfully remove the String array from the ArrayList?

8
  • remove by position should work. Are you using the right index? the return value will be the removed array
    – sidgate
    Oct 6, 2016 at 16:55
  • 2
    I wouldn't expect the first one to work. I would expect the second to work. Show us the code for the second one. Oct 6, 2016 at 16:55
  • @LouisWasserman the second one is this public void eliminarPos(String var, String lex, String valor, Integer ii) { boolean uno=data.remove(ii); System.out.println(uno); } The output is false
    – Baker1562
    Oct 6, 2016 at 17:00
  • Don't use a capital-I Integer. Use an int. Oct 6, 2016 at 17:01
  • does it compile? oh.. just realized list.remove() takes object instead of generic type
    – sidgate
    Oct 6, 2016 at 17:01

3 Answers 3

6

If you take a look at the Javadocs, you'll see that there are 2 different overloaded methods for remove. One takes an Object, the other a primitive integer as position:

remove

public E remove(int index)

Removes the element at the specified position in this list. Shifts any subsequent elements to the left (subtracts one from their indices).

The above takes a primitive integer, the below takes an Object:

remove

public boolean remove(Object o)

Removes the first occurrence of the specified element from this list, if it is present. If the list does not contain the element, it is unchanged...

The reason why you see false is because you supply the wrapper class of primitive int. Integer is an Object, thus Java uses the method that most closely resembles the way you call it, and in this case you call with an Object parameter. Java then uses method signature remove(Object o), and since the specified Integer doesn't exist in the ArrayList, it returns false.

Now, the reason why the latter fails is that you create a new String array, thus a new instance. Since new String[] { var, lex, valor } and new String[] { var, lex, valor } are not referentially equal, Java does not find the same Object in the ArrayList and does not remove the item. The solution is to use primitive int:

public void eliminarPos(String var, String lex, String valor, int ii) {
    String[] uno = data.remove(ii); //Make sure to use String[], as different types are returned
    System.out.println(uno);
}

Java will then use the method that takes in a primitive integer because you pass a primitive integer. The reason why you see incompatible types: String[] cannot be converted to boolean is because remove(int index) returns the actual Object at the position specified, or the removed object, not a boolean like remove(Object o) does.

3
data.remove(new String[]{var, lex, valor});

Will never work because even if the arrays contain the same thing, they are not equal to each other, and the array will not be found in the list.


public void eliminarPos(String var, String lex, String valor, Integer ii) {
    boolean uno=data.remove(ii);
    System.out.println(uno);
}

Will not work because you pass it an Integer instead of an int. An Integer is an object, so it is searching the list for that object. Change it to:

public void eliminarPos(String var, String lex, String valor, int ii) {
    data.remove(ii);
}
1

remove by String array will not work, as list.remove calls equals() on the object. In case of array.equals it is just reference comparison (==).

Remove by position works and you have to specify the right index as primitive int.

public void eliminarPos(String var, String lex, String valor, int ii) {
    String[] uno=data.remove(ii);
    System.out.println(uno);
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.