Matrix Elements Over Diagonal

public class Main {
    public static void main(String[] args) {
        int array[][] = {
                {2, 3, 4, 5, 6},
                {2, 3, 4, 5, 6},
                {2, 3, 4, 5, 6},
                {2, 3, 4, 5, 6},
                {2, 3, 4, 5, 6},
        };
        int i, j;
        {
            for (i = 0; i <5; i++) {
                for (j = 0+i; j < 5; j++) {
                    System.out.print(array[i][j] + " ");


                }
                System.out.println();
            }
        }
    }
}

See: example image

How can I give an aesthetic appearance of a high diagonal? You need to move the space after the line feed.

+4
source share
4 answers

For instance:

for (i = 0; i <5; i++) {
    for (j = 0; j < 5; j++) {
        if( j >= i ) {
            System.out.printf( "   ");
        else {
            System.out.printf( "%3d", array[i][j] );
        }
     }
     System.out.println();
}
+2
source

It's hard to understand your implementation. Try to avoid using hard codes for your variables. Good programmers write code that people can understand.

"Common Excuses Used To Comment Code" , , , , , , . , . , , . .

, :

public static void printDiagonalMatrix(int array[][], int numberOfSpacesBetwenElements) {
        assert numberOfSpacesBetwenElements > 0 : "sizeOfSpaceBetwenNumbers should be > 0";

        int numRows = array.length;
        int numCols  = array[0].length;
        int tabulationSize = 1;
        int tabulationIncrement = numberOfSpacesBetwenElements + 1;

        String spacesBetwenElements = String.format("%" + numberOfSpacesBetwenElements + "s", "");

        StringBuilder out = new StringBuilder();

        for (int row = 0; row < numRows; row++) {

            String tabulation = String.format("%" + tabulationSize + "s", "");

            StringBuilder line = new  StringBuilder(tabulation);
            for (int column = row; column < numCols; column++) {
                line.append(array[row][column]).append(spacesBetwenElements);
            }
            line.append("\n");

            out.append(line);

            tabulationSize += tabulationIncrement;
        }

        System.out.print(out);
    }

:

int numberOfSpacesBetwenElements = 1;
printDiagonalMatrix(array, numberOfSpacesBetwenElements);

OfSpacesBetwenElements = 1

enter image description here

OfSpacesBetwenElements = 5

enter image description here

+1

StringBuilder for append . , .

StringBuilder sb = new StringBuilder();
for (int i = 0; i < 5; i++) {
    for (int j = 0; j < i; j++) {
        sb.append("  ");
    }
    for (int j = i; j < 5; j++) {
        sb.append(array[i][j]).append(' ');
    }
    sb.append('\n');
}
System.out.print(sb.toString());

, , array .

for . , j = 0+i .

Another recommendation, you can improve maintainability by getting the length of the array instead of hard coding. For example,

for (int i = 0; i < array.length; i++) {
    for (int j = i; j < array[i].length; j++) {
0
source
int array[][] = {
        {2, 3, 4, 5, 6},
        {2, 3, 4, 5, 6},
        {2, 3, 4, 5, 6},
        {2, 3, 4, 5, 6},
        {2, 3, 4, 5, 6},
};
int i, j;

StringBuilder prefix = new StringBuilder();

for (i = 0; i < 5; i++) {
  prefix.append(String.format("%" + (i+1) + "s", ""));
  System.out.print(prefix.toString().substring(1));
  for (j = i; j < 5; j++) {
    System.out.print(array[i][j]+" ");
  }
  prefix.setLength(prefix.length() - i);
  System.out.println();
}

If there are more matrix digits, you can use:

int array[][] = {
        {2, 34, 44, 555, 6},
        {2, 34, 44, 555, 6},
        {2, 34, 44, 555, 6},
        {2, 34, 44, 555, 6},
        {2, 34, 44, 555, 6},
};
int i, j;

StringBuilder prefix = new StringBuilder();

for (i = 0; i < 5; i++) {
  int elemLength = i > 0 ? ("" + array[i - 1][i - 1]).length() : 0;
  prefix.append(String.format("%" + (i + elemLength + 1) + "s", ""));
  System.out.print(prefix.toString());
  for (j = i; j < 5; j++) {
    System.out.print(array[i][j]+" ");
  }
  prefix.setLength(prefix.length() - (i + 1));
  System.out.println();
}
0
source

Source: https://habr.com/ru/post/1629692/


All Articles