Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR JAVA

Fill two dimensional array

// To fill two dimenional array with some simbol, e.g. "*"
Scanner scanner = new Scanner(System.in);
int number = scanner.nextInt();

String[][] matrix = new String[number][number];

for (String[] strings : matrix) {
    Arrays.fill(strings, ".");
}

// To modify only middle and/or diagonales:
for (int i = 0; i < matrix.length; i++) {
    for (int j = 0; j < matrix[i].length; j++) {
        if (i == number / 2) {
            matrix[i][j] = "*";
        }
        if (j == number / 2) {
            matrix[i][j] = "*";
        }
        if (i == j) {
            matrix[i][j] = "*";
        }
        if (i == number - j - 1) {
            matrix[i][j] = "*";
        }
    }
}

/*

Result:
* . . * . . * 
. * . * . * . 
. . * * * . . 
* * * * * * * 
. . * * * . . 
. * . * . * . 
* . . * . . * 

*/
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #Fill #dimensional #array
ADD COMMENT
Topic
Name
5+9 =