Answer the question
In order to leave comments, you need to log in
How to convert two dimensional array to one dimensional JAVA?
Faced with the stupidest problem in my endeavors. There is a two-dimensional array, namely a matrix of a fixed size 4 x 4. To implement one algorithm, it is more convenient for me to translate the matrix into a regular sequence of numbers written in a "line". Thank you very much in advance.
Answer the question
In order to leave comments, you need to log in
pseudocode
for (i=0; i<4; i++) {
for(j=0; j<4; j++) {
arr[i*4+j] = matr[i][j]
}
}
Perhaps it would still be better?
int[] firstArray = { 1, 2 };
int[] secondArray = { 3, 4 };
int[] resultArray = new int[4];
System.arraycopy(firstArray, 0, resultArray, 0, 2);
System.arraycopy(secondArray, 0, resultArray, 2, 2);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question