Java 移除arrayList的方法不';行不通

Java 移除arrayList的方法不';行不通,java,arraylist,Java,Arraylist,嗨 我编写了这段代码,通过输出可以得到.remove()方法不起作用的结果a、b、c和d是一些点对象,它们具有x和y成员 下面是a和b以及c和d的值,对于upper,必须删除if语句中的这些值,但它不会 X :59 Y: 143 X :165 Y: 140 X :59 Y: 143 X :165 Y: 140 System.out.println(upper.toString()); for(int i =0;i<upper.size();i++)

嗨 我编写了这段代码,通过输出可以得到
.remove()
方法不起作用的结果
a
b
c
d
是一些
对象,它们具有
x
y
成员

下面是
a和b以及c和d的值,对于
upper
,必须删除if语句中的这些值,但它不会

X :59  Y: 143
X :165  Y: 140
X :59  Y: 143
X :165  Y: 140


   System.out.println(upper.toString());
        for(int i =0;i<upper.size();i++)

            if(upper.get(i)==a||upper.get(i)==b||upper.get(i)==c||upper.get(i)==d){
                upper.remove(i);

            }
        for(int i =0;i<lower.size();i++)

            if(lower.get(i)==a||lower.get(i)==b||lower.get(i)==c||lower.get(i)==d){
                upper.remove(i);
            }



        System.out.println(upper.toString());
        System.out.println(lower.toString());


   first println : [X :108  Y: 89, X :165  Y: 140]

   second println: [X :108  Y: 89, X :165  Y: 140]

   third println :  [X :105  Y: 191]
X:59 Y:143
X:165 Y:140
X:59 Y:143
X:165 Y:140
System.out.println(upper.toString());

对于(int i=0;i如果我正确地阅读了你的问题,你假设
=
将比较两个对象的属性。事实并非如此。
=
告诉你两个引用是否指向相同的对象实例,而不是等价的对象实例

例如:

public class Foo {
    public Foo(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @override
    public boolean equals(Object other) {
        Foo otherFoo;

        if (other == null || !(other instanceof Foo)) { // or you might be more restrictive
            return false;
        }

        otherFoo = (Foo)other);
        return otherFoo.x == this.x && otherFoo.y == this.y;
    }

    @override
    public int hashCode() {
        // ...appropriate implementation of hashCode...
    }
}

Foo a = new Foo(0, 0);
Foo b = new Foo(0, 0);
System.out.println(a == b);      // "false"
System.out.println(a.equals(b)); // "true"

分别考虑:当您在代码< ARLYLIST/<代码>中有两个结果匹配的对象时,您必须删除它们。假设它们在列表中的索引8和9。因此,当<代码> i=8 时,在索引<代码> 8 < /代码>中删除该项,而在<代码> 9 < /代码>中的项现在在<代码> 8 < /代码>。在for循环中NT<代码> i>代码,并在索引<代码> 9代码>下继续新的项目,使第二个未被触碰。如果要在循环时修改列表,请考虑向后循环以避免使用该列表,或者使用.< /p>< p>如果我正在阅读您的问题,那么您假定=< /代码>将比较属性。两个对象的引用。

==
告诉您两个引用是否指向相同的对象实例,而不是等价的对象实例

例如:

public class Foo {
    public Foo(int x, int y) {
        this.x = x;
        this.y = y;
    }

    @override
    public boolean equals(Object other) {
        Foo otherFoo;

        if (other == null || !(other instanceof Foo)) { // or you might be more restrictive
            return false;
        }

        otherFoo = (Foo)other);
        return otherFoo.x == this.x && otherFoo.y == this.y;
    }

    @override
    public int hashCode() {
        // ...appropriate implementation of hashCode...
    }
}

Foo a = new Foo(0, 0);
Foo b = new Foo(0, 0);
System.out.println(a == b);      // "false"
System.out.println(a.equals(b)); // "true"

分别考虑:当您在代码< ARLYLIST/<代码>中有两个结果匹配的对象时,您必须删除它们。假设它们在列表中的索引8和9。因此,当<代码> i=8 时,在索引<代码> 8 < /代码>中删除该项,而在<代码> 9 < /代码>中的项现在在<代码> 8 < /代码>。在for循环中NT<代码> i>代码,并在索引<代码> 9 /代码>中继续新的项目,使第二个未被触碰。如果要在循环时修改列表,请考虑向后循环以避免该问题,或者使用.

两个问题。首先,在迭代时将对象从列表中移除。这不是G。好主意

其次,我认为您误解了Java中的
==
操作符,正如所提到的

这是一种更好的方法来完成您正在尝试做的事情(在您解决了
等于
问题后):


这里有两个问题。首先,在迭代列表时从列表中删除对象。这不是一个好主意

其次,我认为您误解了Java中的
==
操作符,正如所提到的

这是一种更好的方法来完成您正在尝试做的事情(在您解决了
等于
问题后):


正如Eric所暗示的,列表的长度随着项目从列表中移除而改变,在刚刚移除的元素之后,所有值的索引也会随之改变

我不确定“lower”的意义是什么。我注意到循环遍历“lower”试图从“upper”中删除元素。这是故意的吗

这是我的解决方案,它基于一个应该从“upper”中删除的点的“remove”列表。也可以使用原始测试的样式,只是每个==检查必须由一个equals()检查替换

如果从Point类中删除equals(…)实现,则不会从“upper”中删除任何内容,因为测试用例故意使用原始a、b、c和d值的克隆

import java.util.ArrayList;
import java.util.List;

import junit.framework.Assert;

import org.junit.Test;


public class TestArrayList
{
    @Test
    public void testRemove()
    {
        // Test fixture:
        Point a = new Point(115, 70);
        Point b = new Point(139, 66);
        Point c = new Point(195, 111);
        Point d = new Point(144, 165);

        List<Point> upper = new ArrayList<Point>();
        upper.add(a.clone());
        upper.add(b.clone());
        upper.add(c.clone());
        upper.add(d.clone());

        List<Point> remove = new ArrayList<Point>();
        remove.add(a.clone());
        remove.add(b.clone());
        remove.add(c.clone());
        remove.add(d.clone());

        // Assertions:
        Assert.assertTrue(upper.size() == 4);
        Assert.assertTrue(remove.size() == 4);


        // Modified code:
        System.out.println(upper.toString());
        System.out.println(remove.toString());

        for (Point p : remove)
        {
            upper.remove(p);
        }

        System.out.println(upper.toString());
        System.out.println(remove.toString());

        // Assertions:
        Assert.assertTrue(upper.isEmpty());
        Assert.assertTrue(remove.size() == 4);
    }
}

class Point implements Cloneable
{
    public int x;
    public int y;

    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    @Override
    public Point clone()
    {
        return new Point(x, y);
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o)
        {
            return true;
        }
        else if (o instanceof Point)
        {
            Point p = (Point) o;

            return x == p.x && y == p.y;
        }
        else
        {
            return false;
        }
    }

    @Override public String toString()
    {
        return "X: " + x + "  Y: " + y;
    }
}
import java.util.ArrayList;
导入java.util.List;
导入junit.framework.Assert;
导入org.junit.Test;
公共类测试列表
{
@试验
公共void testRemove()
{
//测试夹具:
点a=新点(115,70);
b点=新点(139,66);
点c=新点(195111);
点d=新点(144165);
列表上限=新的ArrayList();
添加(a.clone());
添加(b.clone());
添加(c.clone());
添加(d.clone());
List remove=new ArrayList();
删除.add(a.clone());
删除。添加(b.clone());
删除.add(c.clone());
删除。添加(d.clone());
//断言:
Assert.assertTrue(upper.size()==4);
Assert.assertTrue(remove.size()==4);
//修改代码:
System.out.println(upper.toString());
System.out.println(remove.toString());
对于(点p:删除)
{
上部。移除(p);
}
System.out.println(upper.toString());
System.out.println(remove.toString());
//断言:
Assert.assertTrue(upper.isEmpty());
Assert.assertTrue(remove.size()==4);
}
}
类点实现了可克隆性
{
公共int x;
公共智力;
公共点(整数x,整数y)
{
这个.x=x;
这个。y=y;
}
@凌驾
公共点克隆()
{
返回新点(x,y);
}
@凌驾
公共布尔等于(对象o)
{
if(this==o)
{
返回true;
}
else if(o点实例)
{
点p=(点)o;
返回x==p.x&&y==p.y;
}
其他的
{
返回false;
}
}
@重写公共字符串toString()
{
返回“X:+X+”Y:+Y;
}
}

正如Eric所暗示的,列表的长度会随着项目从列表中移除而改变,而刚移除元素后所有值的索引也会随之改变

我不确定“lower”的意义是什么,我注意到循环遍历“lower”试图删除元素
import java.util.ArrayList;
import java.util.List;

import junit.framework.Assert;

import org.junit.Test;


public class TestArrayList
{
    @Test
    public void testRemove()
    {
        // Test fixture:
        Point a = new Point(115, 70);
        Point b = new Point(139, 66);
        Point c = new Point(195, 111);
        Point d = new Point(144, 165);

        List<Point> upper = new ArrayList<Point>();
        upper.add(a.clone());
        upper.add(b.clone());
        upper.add(c.clone());
        upper.add(d.clone());

        List<Point> remove = new ArrayList<Point>();
        remove.add(a.clone());
        remove.add(b.clone());
        remove.add(c.clone());
        remove.add(d.clone());

        // Assertions:
        Assert.assertTrue(upper.size() == 4);
        Assert.assertTrue(remove.size() == 4);


        // Modified code:
        System.out.println(upper.toString());
        System.out.println(remove.toString());

        for (Point p : remove)
        {
            upper.remove(p);
        }

        System.out.println(upper.toString());
        System.out.println(remove.toString());

        // Assertions:
        Assert.assertTrue(upper.isEmpty());
        Assert.assertTrue(remove.size() == 4);
    }
}

class Point implements Cloneable
{
    public int x;
    public int y;

    public Point(int x, int y)
    {
        this.x = x;
        this.y = y;
    }

    @Override
    public Point clone()
    {
        return new Point(x, y);
    }

    @Override
    public boolean equals(Object o)
    {
        if (this == o)
        {
            return true;
        }
        else if (o instanceof Point)
        {
            Point p = (Point) o;

            return x == p.x && y == p.y;
        }
        else
        {
            return false;
        }
    }

    @Override public String toString()
    {
        return "X: " + x + "  Y: " + y;
    }
}