在java中使用对象循环ifs

  • 本文关键字:对象 循环 ifs java java
  • 更新时间 :
  • 英文 :


基本上,我正在做我的学校项目,我发现循环下面的代码并不像我想象的那么容易,所以我要求你提供解决方案,谢谢。

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();
}
  1. 一个简单的for-i loop,如下所示:

    for(int i=0; i<6; i++){
    if (hrac.getBoundsInParent().intersects(vlna.get(i).getBoundsInParent())) {
    vymaz();
    }
    }
    
  2. 使用IntStream

    IntStream.range(0, 6)
    .filter(i -> hrac.getBoundsInParent().intersects(vlna.get(i).getBoundsInParent()))
    .forEach(i -> vymaz());
    

最新更新