基本上,我正在做我的学校项目,我发现循环下面的代码并不像我想象的那么容易,所以我要求你提供解决方案,谢谢。
if (hrac.getBoundsInParent().intersects(vlna.get(0).getBoundsInParent())) {
vymaz();
}
if (hrac.getBoundsInParent().intersects(vlna.get(1).getBoundsInParent())) {
vymaz();
}
if (hrac.getBoundsInParent().intersects(vlna.get(2).getBoundsInParent())) {
vymaz();
}
if (hrac.getBoundsInParent().intersects(vlna.get(3).getBoundsInParent())) {
vymaz();
}
if (hrac.getBoundsInParent().intersects(vlna.get(4).getBoundsInParent())) {
vymaz();
}
if (hrac.getBoundsInParent().intersects(vlna.get(5).getBoundsInParent())) {
vymaz();
}
-
一个简单的
for-i loop
,如下所示:for(int i=0; i<6; i++){ if (hrac.getBoundsInParent().intersects(vlna.get(i).getBoundsInParent())) { vymaz(); } }
-
使用
IntStream
:IntStream.range(0, 6) .filter(i -> hrac.getBoundsInParent().intersects(vlna.get(i).getBoundsInParent())) .forEach(i -> vymaz());