简体   繁体   English

从arrayList中删除特殊元素

[英]removing special element from an arrayList

Hi I have written this part of code .before the for loop I have this arraylist : 嗨,我已经写了这部分代码。在for循环之前,我有这个arraylist:

[X :49.0 Y: 113.0 , angle :0.0, X :141.0 Y: 106.0 , angle :0.0, X :110.0 Y: 185.0 , angle: 1.0768211289482128, X :99.0 Y: 139.0 , angle: 1.9961041242180873, X :103.0 Y: 126.0 , angle : 2.4208694638343324] which show "x" and "y" and "angle" of some points. [X :49.0 Y: 113.0 , angle :0.0, X :141.0 Y: 106.0 , angle :0.0, X :110.0 Y: 185.0 , angle: 1.0768211289482128, X :99.0 Y: 139.0 , angle: 1.9961041242180873, X :103.0 Y: 126.0 , angle : 2.4208694638343324] ,其中显示了一些点的"x" and "y" and "angle" but in the for loop I want to remove those elements that are more than 但在for循环中,我想删除那些超出

X :110.0 Y: 185.0 , angle: 1.0768211289482128

BUT it print this array for me : 但是它为我打印了这个数组:

[X :49.0 Y: 113.0angle0.0, X :141.0 Y: 106.0 , angle:0.0, X :110.0 Y: 185.0 , angle: 1.0768211289482128, X :103.0 Y: 126.0 , angle: 2.4208694638343324] which is incorrect [X :49.0 Y: 113.0angle0.0, X :141.0 Y: 106.0 , angle:0.0, X :110.0 Y: 185.0 , angle: 1.0768211289482128, X :103.0 Y: 126.0 , angle: 2.4208694638343324] ,这是不正确的

 int size  = list .size();
    for (int i=0;i<size;i++) {
        if (list.get(i).getAngle() > pivot.getAngle() ) {
            list.remove(i);
            size--;

        }
    }

please help me thanks 请帮我谢谢

I recommend you to get an Iterator from the list and use iter.hasNext() , iter.next() and iter.remove() , like this: 我建议您从list获取一个Iterator ,并使用iter.hasNext()iter.next()iter.remove() ,如下所示:

 Iterator<Point> iter = list.iterator();
 while (iter.hasNext())
     if (iter.next().getAngle() > pivot.getAngle())
         iter.remove();

Another option would be to put them in a SortedMap , let the angles represent the keys, and use tailMap(pivot.getAngle()) to get those points with a larger angle than the pivot. 另一个选择是将它们放置在SortedMap ,让角度代表键,然后使用tailMap(pivot.getAngle())来获得角度大于枢轴的那些点。

Whenever you remove an element it shifts the indices of the following elements down by 1. The simplest fix is to go in reverse: 每当删除元素时,它都会将以下元素的索引向下移动1。最简单的解决方法是反向:

for (int i  = list.size() - 1; i >= 0; i--) {
    if (list.get(i).getAngle() > pivot.getAngle() ) {
        list.remove(i);
    }
}

When you remove the element, you should also lower your index value ( i ) as otherwise it will skip the next value - because the values will have shifted. 删除该元素时,还应降低索引值( i ),否则它将跳过下一个值-因为这些值将发生偏移。 For example, imagine you originally had values { a, b, c, d, e } . 例如,假设您最初具有值{ a, b, c, d, e } On the iteration where i==2 , you'd look at c . i==2的迭代中,您将看到c If you then remove c you're left with { a, b, d, e } ... when i is incremented to 3 in the iteration part of the for statement, that means list.get(i) would return e - so you'd never look at d . 如果然后删除c ,则留下{ a, b, d, e } ... ...当for语句的迭代部分中i增至3时,这意味着list.get(i)将返回e因此你永远不会看d

You can fix this by adding an i--; 可以通过添加i--; 解决此问题i--; statement inside your if block... but a simpler approach is to work backwards from the end: if块中的语句...但更简单的方法是从头开始向后工作:

for (int i = list.size() - 1; i >= 0; i--) {
    if (list.get(i).getAngle() > pivot.getAngle()) {
        list.remove(i);
    }
}

Alternatively, you might want to consider creating a new list which only contains the elements you do want to keep: 或者,你可能要考虑创建一个新的列表,它仅包含想保留的元素:

List<Foo> validValues = new ArrayList<Foo>();
for (Foo foo : list) {
    if (foo.getAngle() <= pivot.getAngle()) {
        validValues.add(foo);
    }
}

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

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