The part around the dif is a little bit confusing. But it's less complex as you might think. Before you print a row for a certain key, you just have to print all the blank lines between the last key and the current (if there are some):
public static void print(SortedMap<Integer, int[]> map, PrintWriter writer) {
int last = 1;
for (int key : map.keySet()) {
for (int i = last + 1; i < key; i++) {
writer.println();
}
last = key;
writer.println(Arrays.toString(map.get(key)));
}
}
(For simplicity I just used Arrays.toString(...)
to format a row. Replace it as desired.)