行ベースの多次元配列があります:
/** [row][column]. */
public int[][] tiles;
次のように、この配列を列ベースの配列に変換したいと思います。
/** [column][row]. */
public int[][] tiles;
...しかし、どこから始めればよいか本当にわかりません
行ベースの多次元配列があります:
/** [row][column]. */
public int[][] tiles;
次のように、この配列を列ベースの配列に変換したいと思います。
/** [column][row]. */
public int[][] tiles;
...しかし、どこから始めればよいか本当にわかりません
次の解決策は、実際には転置された配列を印刷するだけでなく、正方形だけでなくすべての長方形の配列に対して機能します。
public int[][] transpose(int[][] array) {
// empty or unset array, nothing do to here
if (array == null || array.length == 0)
return array;
int width = array.length;
int height = array[0].length;
int[][] array_new = new int[height][width];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
array_new[y][x] = array[x][y];
}
}
return array_new;
}
たとえば、次の方法で呼び出す必要があります。
int[][] a = new int[][]{{1, 2, 3, 4}, {5, 6, 7, 8}};
for (int i = 0; i < a.length; i++) {
System.out.print("[");
for (int y = 0; y < a[0].length; y++) {
System.out.print(a[i][y] + ",");
}
System.out.print("]\n");
}
a = transpose(a); // call
System.out.println();
for (int i = 0; i < a.length; i++) {
System.out.print("[");
for (int y = 0; y < a[0].length; y++) {
System.out.print(a[i][y] + ",");
}
System.out.print("]\n");
}
期待どおりの出力になります:
[1,2,3,4,]
[5,6,7,8,]
[1,5,]
[2,6,]
[3,7,]
[4,8,]
これを試して:
@Test
public void transpose() {
final int[][] original = new int[][]{
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}};
for (int i = 0; i < original.length; i++) {
for (int j = 0; j < original[i].length; j++) {
System.out.print(original[i][j] + " ");
}
System.out.print("\n");
}
System.out.print("\n\n matrix transpose:\n");
// transpose
if (original.length > 0) {
for (int i = 0; i < original[0].length; i++) {
for (int j = 0; j < original.length; j++) {
System.out.print(original[j][i] + " ");
}
System.out.print("\n");
}
}
}
出力:
1 2 3 4
5 6 7 8
9 10 11 12
matrix transpose:
1 5 9
2 6 10
3 7 11
4 8 12
もう少し一般的な方法:
/**
* Transposes the given array, swapping rows with columns. The given array might contain arrays as elements that are
* not all of the same length. The returned array will have {@code null} values at those places.
*
* @param <T>
* the type of the array
*
* @param array
* the array
*
* @return the transposed array
*
* @throws NullPointerException
* if the given array is {@code null}
*/
public static <T> T[][] transpose(final T[][] array) {
Objects.requireNonNull(array);
// get y count
final int yCount = Arrays.stream(array).mapToInt(a -> a.length).max().orElse(0);
final int xCount = array.length;
final Class<?> componentType = array.getClass().getComponentType().getComponentType();
@SuppressWarnings("unchecked")
final T[][] newArray = (T[][]) Array.newInstance(componentType, yCount, xCount);
for (int x = 0; x < xCount; x++) {
for (int y = 0; y < yCount; y++) {
if (array[x] == null || y >= array[x].length) break;
newArray[y][x] = array[x][y];
}
}
return newArray;
}
マトリックスのインプレース転置を行いたい場合(この場合row count = col count
)、Javaで次のことができます
public static void inPlaceTranspose(int [][] matrix){
int rows = matrix.length;
int cols = matrix[0].length;
for(int i=0;i<rows;i++){
for(int j=i+1;j<cols;j++){
matrix[i][j] = matrix[i][j] + matrix[j][i];
matrix[j][i] = matrix[i][j] - matrix[j][i];
matrix[i][j] = matrix[i][j] - matrix[j][i];
}
}
}
public int[][] tiles, temp;
// Add values to tiles, wherever you end up doing that, then:
System.arraycopy(tiles, 0, temp, 0, tiles.length);
for (int row = 0; row < tiles.length; row++) // Loop over rows
for (int col = 0; col < tiles[row].length; col++) // Loop over columns
tiles[col][row] = temp[row][col]; // Rotate
それはあなたのためにそれをするはずです。
ここに私の 50 セントがあります: 多次元配列を変換するためのユーティリティ メソッドとテスト (私の場合は double の場合):
/**
* Transponse bidimensional array.
*
* @param original Original table.
* @return Transponsed.
*/
public static double[][] transponse(double[][] original) {
double[][] transponsed = new double
[original[0].length]
[original.length];
for (int i = 0; i < original[0].length; i++) {
for (int j = 0; j < original.length; j++) {
transponsed[i][j] = original[j][i];
}
}
return transponsed;
}
@Test
void aMatrix_OfTwoDimensions_ToBeTransponsed() {
final double[][] original =
new double[][]{{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
double[][] transponsed = Analysis.transponse(original);
assertThat(transponsed[1][2], is(equalTo(10)));
}
public int[][] getTranspose() {
int[][] transpose = new int[row][column];
for (int i = 0; i < row; i++) {
for (int j = 0; j < column; j++) {
transpose[i][j] = original[j][i];
}
}
return transpose;
}