我正在尝试删除周边12为三角形的所有元素,我设置了一个if语句,用for循环检查数组列表中三角形对象的所有周长。
我的问题是,当我运行for循环时,它会删除所有三角形,其周长等于12但是最后一个,它仍将显示在输出中。
代码:
double smallestTriangle = 0.0;
double biggestCircle = 0.0;
/*for loops to get the smallest triangle(12) and largest circle(63.xx)*/
for(int i = 0; i < ShapeList.size(); i++) {
if (ShapeList.get(i).getClass().getName() == "Triangle") {
System.out.println(ShapeList.get(i));
System.out.println(ShapeList.get(i).getPerimeter() + " - " + smallestTriangle);
if (ShapeList.get(i).getPerimeter() == smallestTriangle) {
ShapeList.remove(i);
System.out.println("REMOVED!");
}
}
if (ShapeList.get(i).getClass().getName() == "Circle") {
if (ShapeList.get(i).getPerimeter() == biggestCircle) {
ShapeList.remove(i);
}
}
}
输出:
Triangle {a=3.0 b=4.0 c=5.0} perimeter = 12.0
12.0 - 12.0
REMOVED!
Triangle {a=3.0 b=4.0 c=5.0} perimeter = 12.0
12.0 - 12.0
REMOVED!
Triangle {a=4.9 b=5.0 c=8.9} perimeter = 18.8
18.8 - 12.0
Triangle {a=3.1 b=4.1 c=5.1} perimeter = 12.299999999999999
12.299999999999999 - 12.0
Triangle {a=4.0 b=5.0 c=6.0} perimeter = 15.0
15.0 - 12.0
Triangle {a=3.1 b=4.0 c=5.0} perimeter = 12.1
12.1 - 12.0
Triangle {a=3.9 b=4.8 c=5.7} perimeter = 14.399999999999999
14.399999999999999 - 12.0
Circle {r=1.0} perimeter = 6.283185307179586
Circle {r=2.111} perimeter = 13.263804183456108
Circle {r=1.1} perimeter = 6.911503837897546
Triangle {a=3.9 b=4.0 c=5.9} perimeter = 13.8
Square {l=3.0} perimeter = 12.0
Rectangle {l=3.0 w=5.1} perimeter = 16.2
Square {l=5.0} perimeter = 20.0
Triangle {a=4.9 b=5.0 c=8.9} perimeter = 18.8
Rectangle {l=8.0 w=2.1} perimeter = 20.2
Circle {r=3.8} perimeter = 23.876104167282428
Triangle {a=3.1 b=4.1 c=5.1} perimeter = 12.299999999999999
Triangle {a=4.0 b=5.0 c=6.0} perimeter = 15.0
Rectangle {l=3.1 w=5.2} perimeter = 16.6
Circle {r=10.0} perimeter = 62.83185307179586
Triangle {a=3.1 b=4.0 c=5.0} perimeter = 12.1
Circle {r=2.0} perimeter = 12.566370614359172
Circle {r=3.0} perimeter = 18.84955592153876
Rectangle {l=3.0 w=5.2} perimeter = 16.4
Square {l=100.1} perimeter = 400.4
Square {l=100.2} perimeter = 400.8
Triangle {a=3.0 b=4.0 c=5.0} perimeter = 12.0 // <- not getting removed
Triangle {a=3.9 b=4.8 c=5.7} perimeter = 14.399999999999999
数组列表:
Triangle {a=3.0 b=4.0 c=5.0} perimeter = 12.0
Circle {r=1.0} perimeter = 6.283185307179586
Circle {r=2.111} perimeter = 13.263804183456108
Circle {r=1.1} perimeter = 6.911503837897546
Triangle {a=3.0 b=4.0 c=5.0} perimeter = 12.0
Triangle {a=3.9 b=4.0 c=5.9} perimeter = 13.8
Square {l=3.0} perimeter = 12.0
Rectangle {l=3.0 w=5.1} perimeter = 16.2
Square {l=5.0} perimeter = 20.0
Triangle {a=4.9 b=5.0 c=8.9} perimeter = 18.8
Rectangle {l=8.0 w=2.1} perimeter = 20.2
Circle {r=3.8} perimeter = 23.876104167282428
Triangle {a=3.1 b=4.1 c=5.1} perimeter = 12.299999999999999
Triangle {a=4.0 b=5.0 c=6.0} perimeter = 15.0
Rectangle {l=3.1 w=5.2} perimeter = 16.6
Circle {r=10.0} perimeter = 62.83185307179586
Triangle {a=3.1 b=4.0 c=5.0} perimeter = 12.1
Circle {r=2.0} perimeter = 12.566370614359172
Circle {r=3.0} perimeter = 18.84955592153876
Rectangle {l=3.0 w=5.2} perimeter = 16.4
Square {l=100.1} perimeter = 400.4
Square {l=100.2} perimeter = 400.8
Circle {r=10.1} perimeter = 63.46017160251382
Triangle {a=3.0 b=4.0 c=5.0} perimeter = 12.0 //this element isn't getting removed
Triangle {a=3.9 b=4.8 c=5.7} perimeter = 14.399999999999999
答案 0 :(得分:3)
你的问题是arrayList在每个循环中减小了大小
例如,如果arrayList大小为23,则下一个循环将为22,在这种情况下没有23的索引,我建议你在iterator
上迭代arrayList,或者以最后一个以first结尾的索引开始一个向后循环索引
答案 1 :(得分:1)
你还删除了一些圈子吗?
似乎正在发生的事情是,每次从列表中删除一个形状时,您都会跳过&#34;跳过&#34;下一个。从列表中删除一个元素时,将所有后续元素的索引减1。因此,如果你删除元素22,那么下一个元素的新索引是22,但你最后用一个简单的for循环来滑动它。
你能做的是:
for(int i = ShapeList.size() - 1; i >=0 ; i--) {...}
更新:我重读了您的代码,我非常肯定您必须连续删除两个实体时出现问题。您删除了最后一个三角形之前的最大圆圈,因此在增加索引时跳过它。 我同意Basil Battikhi的说法,对于你的问题来说,反向循环顺序是完美的!
答案 2 :(得分:0)
问题是在使用数组删除后,你的元素向左移动,所以你的索引i在每次删除后都会跳过1个值: 删除此列表中指定位置的元素。将任何后续元素向左移位(从索引中减去一个)。有关详细信息,请查看javadoc。 您可以轻松地为每个不需要的元素位置/索引创建另一个列表,并在之后删除它们。