2010-11-21 100 views
0

嗨 我已经写了这个代码,与输出,你可以得到.remove()方法不起作用。 a,b,cd是一些Points对象有xy成员。删除方法为arrayList不起作用

这里是a and b and c and d值,它在if语句中必须删除upper但它不是。

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] 
+0

必须有一个容易/更好的方式做你想做的事 – 2010-11-21 14:00:19

+0

我编辑了我的问题。 – user472221 2010-11-21 14:04:35

+0

哇。我可以说吗?牙套。具体来说,卷曲的。真。或者你是否主动要求维护程序员在看到这些代码时跳过? ;-) – 2010-11-21 14:07:10

回答

7

如果我正在阅读你的问题,你假设==将比较两个对象的属性。它不,这就是equals==告诉你两个引用是否与相同对象实例,而不是等价的。

因此,例如:

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" 

另外:考虑当你有,你必须删除ArrayList 2名consequtive匹配对象会发生什么。说他们在列表中的索引8和9。因此,当i == 8时,您删除索引8处的物品,而原来位于​​9的物品现在处于8处。但是,然后在for循环中增加i,并继续使用索引9处的新项目,而保持第二个项目不变。如果您想在循环播放列表时修改列表,请考虑向后循环以避免出现这种情况,或者使用Iterator

4

这里有两个问题。首先,您在迭代它时从列表中删除对象。这不是一个好主意。其次,我认为你误解了Java中的==运算符,正如@T.J. Crowder所述。

这是做你正在尝试做的(你已经解决了equals发行后)有什么更好的办法:

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

List<Point> otherPoints = new ArrayList(); 

for(Point p: upper) 
    for(Point myPoint: mypoints) 
    { 
     if(p.equals(myPoint)) 
      break; 

     otherPoints.add(p); 
    } 

upper = otherPoints; 

另一种实现(这只能如果upperSet,因为它不会赶上一式两份):

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

for(Point myPoint: mypoints) 
{ 
    upper.remove(myPoint); 
} 
+0

我已经做到了,它不工作! – user472221 2010-11-21 14:31:16

+0

@ user472221:您是否按照[@ T.J。中所述将'equals()'方法添加到'Point'类中。克劳德的答案](http://stackoverflow.com/questions/4238187/remove-method-for-arraylist-doesnt-work/4238221#4238221)? – Eric 2010-11-21 15:18:43

+0

是的,我已经加入 – user472221 2010-11-22 07:21:42

0

正如埃里克意味着,名单变化项目的长度是从它删除,并刚刚删除的元素后,所以做的所有值的指数。

我不确定“下”的意义是什么。我注意到遍历“较低”的循环尝试从“较高”移除元素。这是故意的吗?

这是我的解决方案,它基于应从“上”移除的点的“删除”列表。除了每个==检查必须由equals()检查代替外,还可以使用原始检测的样式。

如果将equals(...)实现从Point类中移除,则不会从“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; 
    } 
} 
+0

D'oh!我正在查看Java 1.4.2'ArrayList'文档! – Eric 2010-11-21 15:20:23