简体   繁体   English

从对角二维数组中获取元素

[英]Getting element from diagonal 2d array

Hi can someone help me figure out the following question.嗨,有人可以帮我弄清楚以下问题。 I got the 2d array to print out the diagonal elements but its in the wrong direction and i have no clue how to generate the other parts!我得到了二维数组来打印对角线元素,但它的方向错误,我不知道如何生成其他部分!

Fill in an array of N rows and N columns as follows:填写一个 N 行 N 列的数组,如下所示:

• the elements of the last column (except the element of line 0 and column N-1) are created by a random natural number between 0 and 10 limits included. • 最后一列的元素(第 0 行和第 N-1 列的元素除外)由包含在 0 到 10 范围内的随机自然数创建。

• the elements of the first column (except the element of line N-1 and of column 0) are created by a random natural number between 0 and 10 limits included. • 第一列的元素(第 N-1 行和第 0 列的元素除外)由包含在 0 到 10 范围内的随机自然数创建。

• The elements of the diagonal going from the upper right corner to the lower left corner are obtained by calculating the square of the number of the line of the element. • 从右上角到左下角的对角线元素是通过计算元素的行数的平方得到的。

• any other element is obtained: • 获得任何其他元素:

✓ if it is above the diagonal, by adding a random number 0 or 1 or 2 to the element of the same column and of the adjacent lower line. ✓ 如果它在对角线上方,则通过将随机数 0 或 1 或 2 添加到同一列和相邻下一行的元素。

✓ if it is below the diagonal, by adding a random number 1 or 2 or 3 to the element of the same column and of the upper adjacent line. ✓ 如果它在对角线下方,则通过将随机数 1 或 2 或 3 添加到同一列和上相邻行的元素。

HERE YOU CAN SEE WHAT I HAVE SO FAR在这里你可以看到我到目前为止

public class TableauxJuinA {

public static void main(String[] args) {
    // TODO Auto-generated method stub
    //////////////bloc1:decleration des variables////////////
    int n;
    int a;
    /////////////bloc2///////////////////////////////////////
    n = (int)(1+Math.random()*(10)*2+1);
     a = 0;
    int[][]tab = new int [n][n];
    ///////////////////bloc3/?/////////////////
    for (int j = 0; j < n-1; j++) {
        tab[j][n-1] = (int) (Math.random()*10);
    }
    for (int  j = 0; j <tab.length; j++) {
    tab[j][0]=(int)(Math.random()*10);
    for (int diagonale = 0; diagonale <tab.length; diagonale++) {   
        tab[diagonale][diagonale]= (int)(diagonale*diagonale);
    }
    }
    aff(tab,n);
}
private static void aff(int[][] tab, int n ) {
    for (int i = 0; i <n; i++) {
        for (int j = 0; j < n; j++) {
            System.out.print("  [" + tab[i][j] + "]\t");


        }
        System.out.println();

You should think about your array indices very well.你应该很好地考虑你的数组索引。 If I have an 3x3 array like below, the first array (ie [1, 2, 3]) has index 0. So if I want to get the 3, the first number on the diagonal you want, I would have to call array[0][2], the third number (zero-indexed) of the first array.如果我有一个像下面这样的 3x3 数组,第一个数组(即 [1, 2, 3])的索引为 0。所以如果我想得到 3,你想要的对角线上的第一个数字,我必须调用数组[0][2],第一个数组的第三个数字(从零开始)。 Therefore the indices of the diagonal upper-left to bottom-right are indeed array[x][x], but the indices for the upper-right to lower-left are in this case array[0][2], array[1][1] and array[2][0].因此,对角线左上角到右下角的索引确实是 array[x][x],但在这种情况下,右上角到左下角的索引是 array[0][2],array[1 ][1] 和数组[2][0]。 These indices could be called with the for-loop I have defined below.可以使用我在下面定义的 for 循环调用这些索引。

array = [[1, 2, 3],
         [4, 5, 6], 
         [7, 8, 9]]

for (int row = 0; row < array.length; row++) {
    for (int column = array.length - 1; column >= 0; column--) {
         int diagonal = array[row][column]
    }

}

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

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