Skip to content

Instantly share code, notes, and snippets.

@ckob
Last active January 11, 2016 20:41
Show Gist options
  • Select an option

  • Save ckob/40f6df4e574c4f76c6d4 to your computer and use it in GitHub Desktop.

Select an option

Save ckob/40f6df4e574c4f76c6d4 to your computer and use it in GitHub Desktop.
Transpose
import java.util.Arrays;
public class Program {
public static void main (String args[]) {
int[][] o = new int[][]{
{ 1, 20, 10 },
{ 6, 3, 18 }
};
int[][] d = new int[o[0].length][o.length];
for (int i = 0; i < o.length; i++) {
for (int j = 0; j < o[i].length; j++) {
d[j][i]=o[i][j];
}
}
System.out.println(Arrays.deepToString(o));
System.out.println(Arrays.deepToString(d));
}
}
import java.util.Arrays;
public class Program {
public static void main (String args[]) {
int[][] o = new int[][]{
{ 1, 20, 10 },
{ 6, 3, 18 },
{ 30, 2, 7 }
};
System.out.println(Arrays.deepToString(o));
int tmp;
for (int i = 0; i < o.length; i++) {
for (int j = i+1; j < o[i].length; j++) {
tmp = o[i][j];
o[i][j]=o[j][i];
o[j][i]=tmp;
}
}
System.out.println(Arrays.deepToString(o));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment