从 List<String[]> 中删除元素没有按预期工作 [英] Removing an element from a List&lt;String[]&gt; not working as expected

查看:23
本文介绍了从 List<String[]> 中删除元素没有按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个字符串数组的 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆