从 List<String[]> 中删除元素没有按预期工作

编程入门 行业动态 更新时间:2024-10-22 14:41:05
本文介绍了从 List<String[]> 中删除元素没有按预期工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

限时送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:关键词]

本文发布于:2023-04-16 10:24:26,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/884955.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:元素   工作   lt   List   gt

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!