如何在第一行中创建一个具有不同起始值的二维数组?

时间:2019-03-18 05:19:28

标签: java multidimensional-array

我需要帮助学习如何根据第一行中的上方任何行来制作一个具有不同起始值的2d数组,然后我需要将其加倍。 像这样:

每周流行1流行2流行3流行4流行5

0 10 100 500 1000 5000

1 20 200 1000 2000 10000

去9周

2 个答案:

答案 0 :(得分:0)

Two For Loop将完成这项工作。 https://www.programiz.com/java-programming/multidimensional-arrayhttps://www.w3schools.com/java/java_for_loop.asp

public class HelloWorld{

     public static void main(String []args){
        int row = 9; // row
        int col = 5; // col
        int[][] results = new int[row][col]; // final results
        int[] firstvalues = new int[] {10,100,500,1000,5000};

        for(int a = 0 ; a < firstvalues.length; a++ ) {
            System.out.println(firstvalues[a]);
        }
        //first row to last row
        for(int i = 0;i < row; i++ ) {
            //first column to last column
            for(int j = 0; j < col; j++) {
                if(i==0) {
                    results[i][j] = (firstvalues[j]);
                } 
                    results[i][j] = (i + 1) * firstvalues[j];    
            }
        }

        for(int i = 0; i < row; i++ ) {
            for(int j = 0; j < col; j++) {
                System.out.println("Element at row:" + i + " " + j + " " + results[i][j]);
            }
        }
     }
}

答案 1 :(得分:0)

由于答案基本上是由@phyonemyatt提供的,因此以下内容解决了计算问题并提供了格式化输出。尽管如此,基本方法还是@phonemyatt的方法。

  public static void main(String []args){
    int row = 10; // row
    int col = 5; // col
    int[][] results = new int[row][col]; // final results

    // set the first row's starting values
    int[] firstvalues = new int[] {10,100,500,1000,5000};


    //first row to last row
    for(int i = 0;i < row; i++ ) {
        //first column to last column
        for(int j = 0; j < col; j++) {

            // if we are the first row, use the defined starting values
            if(i==0) {
                results[i][j] = (firstvalues[j]);
            } 
            else {
                // double from the row before
                results[i][j] = results[i-1][j] * 2;
            }
        }
    }

    // create the output, we use printf for formatting
    System.out.printf("%5s", "week  ");
    for (int i = 0; i < col; ++i) {
        System.out.printf("%10s", "Pop " + (i + 1));
    }
    System.out.println();

    for(int i = 0; i < row; i++ ) {
        System.out.printf("%4d  ", i);
        for(int j = 0; j < col; j++) {

            System.out.printf("%10d", results[i][j]);
        }
        System.out.println();
    }
 }

输出

 week       Pop 1     Pop 2     Pop 3     Pop 4     Pop 5  
    0          10       100       500      1000      5000  
    1          20       200      1000      2000     10000  
    2          40       400      2000      4000     20000
    3          80       800      4000      8000     40000
    4         160      1600      8000     16000     80000
    5         320      3200     16000     32000    160000
    6         640      6400     32000     64000    320000
    7        1280     12800     64000    128000    640000
    8        2560     25600    128000    256000   1280000
    9        5120     51200    256000    512000   2560000