简体   繁体   中英

Iterating through a 2d array and reading its size

I am trying to make a program which will run through the inputted 2d array. The output should be all values in the 2d array which passed the if statement. 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.

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. 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. 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? 15 years now?

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):

A List of Double could be returned immediately, but an Array needs some conversion first:

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. Unfortunately, there is no one-command conversion in the standard libs between Boxed values and unboxed ones and Arrays/Lists/... thereof:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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