简体   繁体   中英

Why does nested for loops used to generate a 2D array from another 2D array returns 0 for last index of each inner array?

I would like to build an array of columns from this array the goal is to have this method accept a jagged array IE not square or rectangular, like the on in my code example is rectangular for instance. As you can see from my output every 4th index of one the inner arrays equals zero, though it would seem that the value at the time it is a assigned is actually equal to the correct value from the sales array. Notice in the main method that when I output salesByColumn[2][4] that the value is 0.0 (it is the last entry to the console). If you look in the Sales class where the method used to generate this 2D array that when it assigns salesByColumn it does so like salesByColumn[ i ][ j ] , therefore you can determine that when that index is assigned that i = 2 and j = 4 . The method assigns salesByColumn[2][4] to sales[4][2] , which if you observe the sales array sales[4][2] = 2391.0. Inside the method I also print the value of sales[4][2] when the method is called and as you can see at the top of the output it evaluates to 2391.0. So why is it assigning it to 0.0 when the method is called and iterated upon? Thank you for your time and consideration.

MAIN CLASS

public class Main {

    public static void main(String[] args) {
        double[][] salesByColumn = Sales.salesByColumn();
        for(double[] column : salesByColumn) {
            for(double sale : column) {
                System.out.println(sale);
            }
            System.out.println();
        }

        System.out.println(salesByColumn[2][4]);
    }

}

SALES CLASS

public class Sales {

    static double[][] sales = {
        {1540.0, 2010.0, 2450.0, 1845.0},
        {1130.0, 1168.0, 1847.0, 1491.0}, 
        {1580.0, 2305.0, 2710.0, 1284.0}, 
        {1105.0, 4102.0, 2391.0, 1576.0},
        {1105.0, 4102.0, 2391.0, 1576.0}
    }

    public static double[][] salesByColumn() {
        int maxColumns = 0;
        for(double[] row : sales) {
            if(row.length > maxColumns) {
                maxColumns = row.length;
            }
        }

        System.out.println("Look here " + sales[4][2]);

        double[][] salesByColumn = new double[maxColumns][];
        for(int i = 0; i < maxColumns; i++) {
            salesByColumn[i] = new double[(sales.length)];
            for(int j = 0; j < sales[i].length; j++) {
                salesByColumn[i][j] = sales[j][i];
            }
        }
        return salesByColumn;
    }
}

OUTPUT

run:

Look here 2391.0
1540.0
1130.0
1580.0
1105.0
0.0

2010.0
1168.0
2305.0
4102.0
0.0

2450.0
1847.0
2710.0
2391.0
0.0

1845.0
1491.0
1284.0
1576.0
0.0

0.0
BUILD SUCCESSFUL (total time: 0 seconds)

The index in your inner loop is incorrect. You don't want to iterate over the length of sales[i] , you need the total number of rows so that you can turn each row into a column.

Here is a working loop

        System.out.println("Look here " + sales[4][2]);

        double[][] salesByColumn = new double[maxColumns][];
        for(int i = 0; i < maxColumns; i++) {
            salesByColumn[i] = new double[(sales.length)];
            for(int j = 0; j < sales.length; j++) {
                System.out.println("Assigning "+i+", "+j+" to" +sales[j][i]);
                salesByColumn[i][j] = sales[j][i];
            }
        }

If your real input data is truly jagged as you mention then you'll need an index check in the inner loop.

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