TransposeUtils { public static <T> List<List<T>> transpose(List<List<T>> mat) { int height = mat.size(); int width = mat.get(0).size(); List<List<T>> transposed = new ArrayList<>(width); for (int w = 0; w < width; w++) { List<T> newRow = new ArrayList<>(height); for (List<T> aMat : mat) { newRow.add(aMat.get(w)); } transposed.add(newRow); } return transposed; } private  TransposeUtils(); static List<List<T>> transpose(List<List<T>> mat);  }
@Test public void testTranspose() { List<List<Integer>> mat = new ArrayList<>(4); mat.add(Arrays.asList(1, 2, 3)); mat.add(Arrays.asList(4, 5, 6)); mat.add(Arrays.asList(7, 8, 9)); mat.add(Arrays.asList(10, 11, 12)); List<List<Integer>> expected = new ArrayList<>(3); expected.add(Arrays.asList(1, 4, 7, 10)); expected.add(Arrays.asList(2, 5, 8, 11)); expected.add(Arrays.asList(3, 6, 9, 12)); List<List<Integer>> actual = TransposeUtils.transpose(mat); assertEquals(expected, actual); }