简体   繁体   English

为什么用于从另一个二维数组生成二维数组的嵌套 for 循环为每个内部数组的最后一个索引返回 0?

[英]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.我想从这个数组中构建一个列数组,目标是让这个方法接受一个锯齿状的array IE,而不是正方形或矩形,例如我的代码示例中的 on 是矩形。 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.正如您从我的输出中看到的,内部数组的每 4 个索引都等于 0,尽管它被分配时的值似乎实际上等于 sales 数组中的正确值。 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).请注意,在 main 方法中,当我输出salesByColumn[2][4] ,该值为0.0 (它是控制台的最后一个条目)。 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 .如果您查看 Sales 类中用于生成此二维数组的方法,当它分配 salesByColumn 时,它的作用类似于salesByColumn[ i ][ j ] ,因此您可以确定当分配该索引时i = 2j = 4 . The method assigns salesByColumn[2][4] to sales[4][2] , which if you observe the sales array sales[4][2] = 2391.0.该方法将salesByColumn[2][4]分配给sales[4][2] ,如果您观察 sales 数组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.在方法内部,我还在调用该方法时打印sales[4][2]的值,正如您在输出顶部看到的那样,它的计算结果为2391.0. So why is it assigning it to 0.0 when the method is called and iterated upon?那么为什么在调用和迭代该方法时将其分配给 0.0 呢? 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.您不想遍历sales[i]的长度,您需要总行数,以便您可以将每一行变成一列。

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.如果您的真实输入数据确实像您提到的那样参差不齐,那么您将需要在内循环中进行索引检查。

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

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