迭代2d数组并读取其大小

时间:2018-04-04 01:27:54

标签: java arraylist

我正在尝试创建一个将通过输入的2d数组运行的程序。输出应该是传递if语句的2d数组中的所有值。双精度输出数组应该是一个可以适合正确值的大小。我有一个for循环来确定大小,然后另一个用于添加正确的值。

public static double[] getAreasGreaterThan(GeometricShape[][] haystack, double threshold) {
    // TODO
    int count = 0;
    for (int a = 0; a < haystack.length; a++) {
        for (int b = 0; b < haystack[a].length; b++) {
            if(haystack[a][b].getArea() > threshold) {
                count++;
            }
         }       
    }
    double[] areas = new double[count];
    for (int i = 0; i < haystack.length; i++) {
        for (int j  =0; j < haystack[i].length; j++) {
            if(haystack[i][j].getArea() > threshold) {
                areas[i] = haystack[i][j].getArea();
            }        
        }
    }
    return areas;
}

我不断超出界限或仅接收错误的输出。我的迭代错了吗?

3 个答案:

答案 0 :(得分:2)

我认为你也可以尝试另一种方式,将输出放在一个列表中,然后转换为数组,这样会更好理解。像这样:

       List<Double> areaList = new ArrayList<Double>();
       for (int a = 0; a < haystack.length; a++) {
          for (int b = 0; b < haystack[a].length; b++) {
              if(haystack[a][b].getArea() > threshold) {
              areaList.add(haystack[a][b].getArea());
              }
          }       
       }
       return areaList.toArray(new Double[areaList.size()]);

答案 1 :(得分:1)

问题出在这里,你没有正确地遍历区域。你应该有一个单独的计数器,用于值应该去的区域。当您的i值超过可能的对象数时,您的错误会弹出,只要您的i维度超过区域数量,就会发生这种情况。例如,当第一个维度的长度为7时,您只有3个对象可以通过,而最后一个对象在任何第一个维度中超过3,您将收到错误。如果错误仍然存​​在,请告诉我。

int areasIterable=0
for (int i = 0; i < haystack.length; i++) {
    for (int j  =0; j < haystack[i].length; j++) {
        if(haystack[i][j].getArea() > threshold) {
            areas[areasIterable] = haystack[i][j].getArea();
            areasIterable=areasIterable+1;
        }        
    }
}

答案 2 :(得分:0)

我们有多少年简化for循环? 15年了?

double[] getAreasGreaterThan(GeometricShape[][] haystack, double threshold) {
    int count = 0;
    for (GeometricShape[] gsa: haystack) {
        for (GeometricShape gs: gsa) {
            if (gs.getArea () > threshold) {
                count++;
            }
         }
    }
    double[] areas = new double[count];
    int i = 0;
    for (GeometricShape[] gsa: haystack) {
        for (GeometricShape gs: gsa) {
            if (gs.getArea () > threshold) {
                areas[i] = gs.getArea();
                i++;
            }
        }
    }
    return areas;
}

不幸的是,在声明数组之前我们需要大小。但是我们可以将有趣的值一次性存储在List(或Vector,或Set,或者哪个Collection可能更适合)中:

可以立即返回Double列表,但是数组首先需要进行一些转换:

Double[] getAreasGreaterThan(GeometricShape[][] haystack, double threshold) {
    List <Double> areas = new ArrayList <> ();
    for (GeometricShape[] gsa: haystack) {
        for (GeometricShape gs: gsa) {
            double area = gs.getArea ();
            if (area > threshold) {
                areas.add (area);
            }
         }
    }
    Double[] areasAd = new Double[areas.size ()];
    areas.toArray (areasAd);
    return areasAd;
}

但这是一个双打数组,可能不是你需要的 - 也许你受到外国或自己的API的约束。不幸的是,在Boxed值和未装箱的值以及Arrays / Lists / ...之间的标准库中没有单命令转换:

double[] getAreasGreaterThan(GeometricShape[][] haystack, double threshold) {
    List <Double> areas = new ArrayList <> ();
    for (GeometricShape[] gsa: haystack) {
        for (GeometricShape gs: gsa) {
            double area = gs.getArea ();
            if (area > threshold) {
                areas.add (area);
            }
         }
    }   
    double[] areasa = new double [areas.size()];
    int i = 0; for (Double d: areas) {areasa [i] = d.doubleValue(); ++i;}
    return areasa;
}