简体   繁体   English

从列表中删除元素 <String[]> 不能按预期工作

[英]Removing an element from a List<String[]> not working as expected

I have an ArrayList of String arrays which is added to like so: 我有一个String数组的ArrayList像这样添加:

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. 尝试擦除String数组之一时,由于某种原因,输出为false 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 . 上面方法的输出为false Is there any way to successfully remove the String array from the ArrayList? 有什么方法可以成功地从ArrayList中删除String数组吗?

If you take a look at the Javadocs , you'll see that there are 2 different overloaded methods for remove . 如果您看一下Javadocs ,就会发现有两种不同的重载方法可供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 . 看到false的原因是因为您提供了原始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. Integer是一个Object ,因此Java使用与调用它的方式最相似的方法,在这种情况下,您使用Object参数进行调用。 Java then uses method signature remove(Object o) , and since the specified Integer doesn't exist in the ArrayList, it returns false . Java然后使用方法签名remove(Object o) ,并且由于指定的Integer在ArrayList中不存在,因此它返回false

Now, the reason why the latter fails is that you create a new String array, thus a new instance. 现在,后者失败的原因是您创建了一个新的String数组,从而创建了一个新实例。 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. 由于new String[] { var, lex, valor }new String[] { var, lex, valor }引用上不相等,因此Java在ArrayList中找不到相同的对象,因此不会删除该项目。 The solution is to use primitive int : 解决方案是使用原始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. 然后,Java将使用接收原始整数的方法,因为您传递了原始整数。 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. 之所以看到incompatible types: String[] cannot be converted to boolean是因为remove(int index) 在指定的位置返回实际的Object ,或者被删除的对象,而不是像remove(Object o)那样的boolean

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 . 将不起作用,因为您将其传递给Integer而不是int An Integer is an object, so it is searching the list for that object. Integer是一个对象,因此它正在列表中搜索该对象。 Change it to: 更改为:

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

remove by String array will not work, as list.remove calls equals() on the object. 按字符串remove数组将不起作用,因为list.remove在对象上调用equals() In case of array.equals it is just reference comparison ( == ). 在array.equals的情况下,它只是引用比较( == )。

Remove by position works and you have to specify the right index as primitive int . 按位置删除作品,您必须将正确的索引指定为原始int

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

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

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