问题描述
限时送ChatGPT账号..我有一个字符串数组的 ArrayList,它被添加到像这样:
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});
当尝试擦除其中一个字符串数组时,由于某种原因,输出为 false
.这是我删除它的方法:
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);
}
上述方法的输出是false
.有没有什么办法可以成功地从ArrayList中取出String数组?
The output for the method above is false
. Is there any way to successfully remove the String array from the ArrayList?
推荐答案
remove
by String array 将不起作用,因为 list.remove
调用 equals()
在对象上.在 array.equals 的情况下,它只是引用比较 (==
).
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 (==
).
按位置删除有效,您必须将正确的索引指定为原始int
.
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);
}
这篇关于从 List<String[]> 中删除元素没有按预期工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
更多推荐
[db:关键词]
发布评论