这并非太困难——假设各行各有同等规模!
private void dumpColumnwise() {
// generate test input
// String[] firstRow = {"a00", "a01", "a02"};
// String[] secondRow = {"a10", "a11", "a12"};
String[] firstRow = "a00;a01;a02".split(";");
String[] secondRow = "a10;a11;a12".split(";");
List<String[]> rows = new ArrayList<String[]>();
rows.add(firstRow);
rows.add(secondRow);
// get longest row
int numberOfColumns = 0;
for (String[] row:rows) {
numberOfColumns = Math.max(numberOfColumns, row.length);
}
int numberOfRows = rows.size();
for (int column = 0; column < numberOfColumns; colunm++) {
for (int row = 0; row < numberOfRows; row++) {
String[] row = rows.get(row);
if (column < row.length) {
System.out.printf(" %s", rows.get(row)[column]);
}
}
}
}
Room for 改进:允许输入数据,长度各异。 在这种情况下,如果一栏指数对实际增长有效,则须(1)确定最长的行刑和(2)测试。