简体   繁体   English

填写2D阵列中的主要对角线

[英]Filling in the main diagonals in a 2D array

I am trying to write a code that fills in the 2 main diagonals in an NxN matrix, for example: if N=5 (which is entered through command line), we would have a 5x5 matrix filled with zeros and the diagonals would have 2s filled in, like: 我正在尝试编写一个代码,该代码填充NxN矩阵中的2个主对角线,例如:如果N = 5(通过命令行输入),我们将有一个5x5的矩阵,其中零填充且对角线为2s填写,例如:

2 0 0 0 2
0 2 0 2 0
0 0 2 0 0
0 2 0 2 0
2 0 0 0 2

I wrote a code for an all-zero table, but i can't figure our how to fill in the diagonals. 我为全零表编写了代码,但无法弄清楚如何填充对角线。 Looking at the case of 5x5 i would have to fill in the matrix at the following indices: 查看5x5的情况,我必须在以下索引处填写矩阵:

#1 (0,0)  (0,n-1) 
#2 (1,1) (1,n-2)
#3 (2,2) (2,n-3) 
#4 (3,1) (3, n-2)
#5 (4,0) (4,n-1)

However, since N can be any number, i assume that first i have to find the middle row, after which i have to decrement the indices in the reverse order. 但是,由于N可以是任何数字,因此我假设首先我必须找到中间行,然后我必须以相反的顺序递减索引。

I am learning Java for 2 weeks only and this one is pretty hard. 我只学习Java 2个星期,这很难。 My code for a zero-filled table is this: 我的零填充表代码是这样的:

public static void main (String[] args){
        int n = Integer.parseInt(args[0]);
        System.out.println(n);
        int[][] table = new int[n][]; 
         for (int i = 0; i < n; i++) { 
         table[i] = new int[i + 1]; 
         for (int j = 0; j <= i; j++) { 
         table[i][j] = (0); 


            }
        } System.out.print(Arrays.deepToString(table));
    }

Obviously, this is very far from what i need to achieve, and i am not sure if it's entirely right. 显然,这与我需要实现的目标相去甚远,而且我不确定这是否完全正确。 I would really appreciate some help. 我真的很感谢您的帮助。

Try this, 尝试这个,

/* package whatever; // don't place package name! */

import java.util.*;
import java.lang.*;
import java.io.*;

/* Name of the class has to be "Main" only if the class is public. */
class Ideone
{
    public static void main (String[] args){
        int n = 5;
        System.out.println(n);
        int[][] table = new int[n][]; 
         for (int i = 0; i < n; i++) { 
         table[i] = new int[n]; 
         for (int j = 0; j < n; j++) {
            if(i==j || n-i == j+1){
                table[i][j] = table[i][n-i-1] = 2;  
            }
            else{
            table[i][j] = 0; 
            }
            System.out.print(table[i][j]);
            }
            System.out.println();
        } 
    }
}

To fill one diagonal, we just have to count from i==0 to i==size-1 and fill in (i,i) each time. 要填充一个对角线,我们只需从i==0i==size-1进行计数,然后每次填充(i,i)

 for(int i=0; i<size; i++) {
     table[i][i] = 2;
 }

The other diagonal is only slightly harder: 另一个对角线仅稍微难一点:

 for(int i=0; i<size; i++) {
     table[i][some calculation involving i and size] = 2;
 }

You should be able to work out what the calculation is. 您应该能够算出什么是计算量。

You can do it in two loops - but you can also combine filling both diagonals into one loop. 您可以分两个循环进行操作-但也可以将填充两个对角线组合成一个循环。

You could also amend your nested loops that create the array, to handle the cells on the diagonals as you encounter them there. 您还可以修改创建数组的嵌套循环,以处理对角线上的单元格。

  #inside the loop
  if( a condition indicating that the cell is on a diagonal) {
      table[i][j] = 2;
  } else {
      table[i][j] = 0;
  }

Write down the coordinates of the cells on the diagonals, and you should quickly see what the condition is. 写下对角线上单元格的坐标,您应该很快了解情况。

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

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