Removing an element from a List not working as expected

前端 未结 3 1199
我寻月下人不归
我寻月下人不归 2021-01-24 21:54

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

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


        
3条回答
  •  执念已碎
    2021-01-24 22:41

    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);
    }
    

提交回复
热议问题