Search
 
SCRIPT & CODE EXAMPLE
 

JAVA

fill two dimensional array

//this way u can fill your array row by row

Scanner input = new Scanner(System.in);
for (int i = 0; i < row; i++){
	for (int j = 0; j < column; j++){
        array[i][j] = input.nextInt();
	}
}

//this way u can fill your array column by column
//Scanner input = new Scanner(System.in);
for (int i = 0; i < column; i++){
	for (int j = 0; j < row; j++){
        array[i][j] = input.nextInt();
	}
}
Comment

fill two dimensional array c++

//this way you can fill your array row by row
for (int i = 0; i < row; i++){
	for (int j = 0; j < column; j++){
		cin >> x;
        array[i][j] = x;
	}
}

//	[ 1 2 3 ]
//	[ 4 5 6 ]
//	[ 7 8 9 ]

//this way you can fill your array column by column

for (int i = 0; i < column; i++){
	for (int j = 0; j < row; j++){
		cin >> x;
        array[i][j] = x;
	}
}

// [ 1 4 7 ]
// [ 2 5 8 ]
// [ 3 6 9 ]
Comment

Fill 2-dimensional array with value

int a[100000][100000];
std::fill((int*)a,(int*)a+sizeof(a)/sizeof(int),0);
Comment

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:
* . . * . . * 
. * . * . * . 
. . * * * . . 
* * * * * * * 
. . * * * . . 
. * . * . * . 
* . . * . . * 

*/
Comment

PREVIOUS NEXT
Code Example
Java :: guessing game in java 
Java :: How to find the logged-in user in Spring Boot? 
Java :: string to stream java 
Java :: open website from android activity 
Java :: Java Creating a Vector 
Java :: java calendar add minutes 
Java :: java extract zip 
Java :: template competitive programming java 
Java :: set source image in android studio 
Java :: integer.tostring java 
Java :: intellisense for eclipse java 
Java :: matrice java 
Java :: java iterator to stream 
Java :: java string not equal 
Java :: classcastexception in java 
Java :: armstrong number in java 
Java :: simple java code 
Java :: java.lang.object[4] 
Java :: loop hash map 
Java :: java final meaning 
Java :: java.lang.ArrayIndexOutOfBoundsException 
Java :: render problem in android studio 
Java :: java random double between 0 and 1 
Java :: android menu change text color 
Java :: distance point to line java 
Java :: java decompiler 
Java :: download File java from website 
Java :: java substring based on delimiter 
Java :: Imageview on the bottom left of Imageview android anchor 
Java :: how to run java in eclipse 
ADD CONTENT
Topic
Content
Source link
Name
7+6 =