简体   繁体   English

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

[英]Iterating through a 2d array and reading its size

I am trying to make a program which will run through the inputted 2d array. 我正在尝试制作一个将通过输入的2d数组运行的程序。 The output should be all values in the 2d array which passed the if statement. 输出应该是传递if语句的2d数组中的所有值。 The output array of doubles should be a size which can fit correct values. 双精度输出数组应该是一个可以适合正确值的大小。 I have one for loop to determine the size and then another which adds correct values. 我有一个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;
}

I keep getting out of bounds exceptions or just receiving the wrong outputs. 我不断超出界限或只是接收错误的输出。 Is my iteration wrong? 我的迭代错了吗?

i think you can also try another way, put the output in a list, then conver to array, this will be better understand. 我想你也可以尝试另一种方式,将输出放在一个列表中,然后转换为数组,这将更好地理解。 like this: 像这样:

       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()]);

The issue is here, you are not iterating through areas correctly. 问题出在这里,你没有正确地遍历区域。 You should have a separate counter for where in areas the value should go. 你应该有一个单独的计数器,用于值应该去的区域。 Your error is popping up when your i value is past the number of possible objects, which will happen whenever your i dimension is longer than the number of areas. 当您的i值超过可能的对象数时,您的错误会弹出,只要您的i维度超过区域数量,就会发生这种情况。 For example, when you the first dimension is of length 7, you have only 3 objects that pass, and the last object is in any first dimension past 3 you will get an error. 例如,当第一个维度的长度为7时,您只有3个对象可以通过,而最后一个对象在任何第一个维度中超过3,您将收到错误。 Let me know if the error persists. 如果错误仍然存​​在,请告诉我。

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;
        }        
    }
}

How many years do we have simplified for-loops? 我们有多少年简化for循环? 15 years now? 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;
}

Unfortunately, we need the size before declaring an array. 不幸的是,在声明数组之前我们需要大小。 But we could store the interesting values in one go in a List (or Vector, or Set, or which Collection might be suitable further on): 但是我们可以将有趣的值一次性存储在List(或Vector,或Set,或者哪个Collection可能更适合)中:

A List of Double could be returned immediately, but an Array needs some conversion first: 可以立即返回Double的List,但是Array需要先进行一些转换:

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;
}

But this is an Array of Doubles, and might not be what you need - maybe you're bound by foreign or own APIs. 但这是一个双打数组,可能不是你需要的 - 也许你受到外国或自己的API的约束。 Unfortunately, there is no one-command conversion in the standard libs between Boxed values and unboxed ones and Arrays/Lists/... thereof: 不幸的是,在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;
}

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

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