I
I
Ilya Balabanov2015-05-26 14:12:05
Java
Ilya Balabanov, 2015-05-26 14:12:05

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

2 answer(s)
V
Vit, 2015-05-26
@ilyablbnv

pseudocode

for (i=0; i<4; i++) {
  for(j=0; j<4; j++) {
    arr[i*4+j] = matr[i][j]
  }
}

E
Evgeny Vasilenko, 2015-05-27
@Lucky_spirit

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 question

Ask a Question

731 491 924 answers to any question