i have a code that create 2d array using user input and it work fine but now i have 2 questions
the first one: how to convert 2d array to 1d array?
second question: how to choose or trace the elements above the right diagonal in the 2d array?
anyone can help me to fix this code?
this my code
package question3;
import java.util.Arrays;
import java.util.Collection;
import java.util.Scanner;
public class Array2d {
public static void main(String[] args) {
int[][] matrix = new int[3][3];
int[] array = new int[matrix.length * matrix.length];
Scanner sc = new Scanner(System.in);
System.out.print("Please enter 9 integers separated by spaces:");
for (int i = 0; i < matrix.length; i++) {
for (int j = 0; j < matrix.length; j++) {
matrix[i][j] = sc.nextInt();
}
}
int idx = 0;
for (int row = 0; row < matrix.length; row++) {
for (int column = 0; column < matrix.length; column++) {
System.out.print(matrix[row][column] + " "); // Outputs the // array in a // 5x5 grid.
}
System.out.println();
}
for (int column = 0; column < matrix.length; column++) {
for (int row = column + 1; row < matrix.length+column ; row++){
// populate your array here
array[idx] = matrix[row][column];
// increment index
idx++;
System.out.println(matrix[row][column]);
}
}
}
}
output
Please enter 9 integers separated by spaces: 1 2 3 4 5 6 7 8 9
1 2 3
4 5 6
7 8 9
4 7 8
but what i expect 2 , 3 , 6
where the change that i need to make because i am stuck and i know that is in the third for loop