Skip to content

Instantly share code, notes, and snippets.

@abdorah
Last active September 5, 2021 22:04
Show Gist options
  • Save abdorah/fef8fad56df1da8c950bebd3897a7d13 to your computer and use it in GitHub Desktop.
Save abdorah/fef8fad56df1da8c950bebd3897a7d13 to your computer and use it in GitHub Desktop.
How to reshape an int[][] matrix in java into int[] array?

This Java class will help you to work on matrixes as if they were a lists. You already know that a matrix is just a list of lists. However, this remark can be very benefical if you knew how to avail yourself of it. This example will help you to do so. In this example you will find various methods that allow you to run through a matrix as if it was a list. Indeed, this may be helpful in case of simple competitive programming problems that involve matrixes or 2D spaces processing.

If you were interested in an example of implementation of this class, you can check this solution for a 2D 8 * 8 space problem , and the problem

package com;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Reshaper {
public int getRowsNumber(int[][] matrix) {
return matrix.length;
}
public int getColumnsNumber(int[][] matrix) {
return matrix[0].length;
}
public int getLength(int[][] matrix) {
return getRowsNumber(matrix) * getColumnsNumber(matrix);
}
public int[] getReshapedMatrix(int[][] matrix) {
List<Integer> reshapedMatrix = new ArrayList<>();
for (int i = 0; i < matrix.length; i++) {
Arrays.stream(matrix[i]).forEach(e -> reshapedMatrix.add(e));
}
return reshapedMatrix.stream().mapToInt(Integer::intValue).toArray();
}
public int upperLineCelle(int[][] matrix, int index) {
return index < getRowsNumber(matrix) ? null : getReshapedMatrix(matrix)[index - getRowsNumber(matrix)];
}
public int lowerLineCelle(int[][] matrix, int index) {
return index > getColumnsNumber(matrix) * getRowsNumber(matrix) ? null
: getReshapedMatrix(matrix)[index + getRowsNumber(matrix)];
}
public int leftSideCelle(int[][] matrix, int index) {
return index % getColumnsNumber(matrix) == 0 ? null : getReshapedMatrix(matrix)[index - 1];
}
public int rightSideCelle(int[][] matrix, int index) {
return index % getColumnsNumber(matrix) == getColumnsNumber(matrix) - 1 ? null
: getReshapedMatrix(matrix)[index + 1];
}
public int upperRightCelle(int[][] matrix, int index) {
return index < getRowsNumber(matrix) && index % getColumnsNumber(matrix) == getColumnsNumber(matrix) - 1 ? null
: getReshapedMatrix(matrix)[index - getRowsNumber(matrix) + 1];
}
public int upperLeftCelle(int[][] matrix, int index) {
return index < getRowsNumber(matrix) && index % getColumnsNumber(matrix) == 0 ? null
: getReshapedMatrix(matrix)[index - getRowsNumber(matrix) - 1];
}
public int lowerRightCelle(int[][] matrix, int index) {
return index > getColumnsNumber(matrix) * getRowsNumber(matrix)
&& index % getColumnsNumber(matrix) == getColumnsNumber(matrix) - 1 ? null
: getReshapedMatrix(matrix)[index + getRowsNumber(matrix) + 1];
}
public int LowerLeftCelle(int[][] matrix, int index) {
return index > getColumnsNumber(matrix) * getRowsNumber(matrix) && index % getColumnsNumber(matrix) == 0 ? null
: getReshapedMatrix(matrix)[index + getRowsNumber(matrix) - 1];
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment