简体   繁体   English

Arraylists的Arraylists中的remove()不起作用

[英]remove() in Arraylists of Arraylists not working

I'm declaring the following two classes: 我声明了以下两个类:

   public class formula
    {
    ArrayList<values> var= new ArrayList<values>;
}

public class values
{
    int val;
    void addval(int val1)
    {
        val=val1;
    }
}

I am declaring an ArrayList of type formula. 我正在声明类型公式的ArrayList。

 ArrayList<formula> S= new ArrayList<formula>;

When I try to execute the following statement: 当我尝试执行以下语句时:

  S.get(i).var.remove(b);

where i is a loop variable i是一个循环变量

I don't get any error and it compiles fine, but doesn't delete the instance of var(b) for formula(i) . 我没有收到任何错误,并且编译良好,但是没有删除formula(i)var(b)实例。 The value still remains. 该值仍然保留。 What is wrong? 怎么了? I am not using Iterators at all, just a loop to traverse through all the instances of formula() . 我根本没有使用Iterators,只是一个循环遍历Formula formula()所有实例。

b is an integer. b是整数。 Essentially the index of the element in the var arraylist I'm trying to delete. 本质上是我要删除的var arraylist中元素的索引。

You must override equals and hashcode while using collection API's. 使用集合API时,您必须覆盖equals和hashcode。 Otherwise you will see lot of unpredictable results in your code... 否则,您将在代码中看到很多无法预测的结果...

If you are new to Java, make it a habit to override equals, hashcode and toString methods. 如果您不熟悉Java,请养成重写equals,hashcode和toString方法的习惯。

If b = the key, it better be a values object. 如果b =键,则最好是一个对象。 Here the object b should have the the same hashCode as the key and the equals() method should return true. 在这里,对象b应该具有与键相同的hashCode,并且equals()方法应该返回true。 In Java for two objects obj1 and obj2 to be be considered as the same, the following conditions should be satisfied. 在Java中,将两个对象obj1obj2视为相同,应满足以下条件。

  1. obj1.hashCode()== obj2.hashCode() obj1.hashCode()== obj2.hashCode()
  2. obj1.equals(obj2)== true obj1.equals(obj2)== true

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

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