花式的Java 8解決方案
import java.util.List;
import java.util.stream.IntStream;
import static java.lang.System.lineSeparator;
import static java.util.Arrays.asList;
import static java.util.stream.Collectors.joining;
public class Example {
public static void main(String[] args) {
List<List<Integer>> matrix = asList(
asList(0, 0, 0, 1),
asList(0, 0, 0, 0),
asList(0, 1, 1, 1),
asList(1, 1, 0, 0),
asList(0, 0, 0, 0)
);
System.out.println("Example output:");
System.out.println(IntStream.range(0, matrix.size())
.mapToObj(i -> (char) ('A' + i) + ": [" + matrix.get(i)
.stream()
.map(String::valueOf)
.collect(joining(", "))
+ "]"
)
.collect(joining(lineSeparator())));
}
}
輸出
Example output:
A: [0, 0, 0, 1]
B: [0, 0, 0, 0]
C: [0, 1, 1, 1]
D: [1, 1, 0, 0]
E: [0, 0, 0, 0]
作爲一個大的班輪,因爲爲什麼不
System.out.println(((Function<List<List<Integer>>, String>) (matrix -> "Example output: " + lineSeparator() + range(0, matrix.size()).mapToObj(i -> (char) ('A' + i) + ": [" + matrix.get(i).stream().map(String::valueOf).collect(joining(", ")) + "]").collect(joining(lineSeparator())))).apply(asList(asList(0, 0, 0, 1), asList(0, 0, 0, 0), asList(0, 1, 1, 1), asList(1, 1, 0, 0), asList(0, 0, 0, 0))));
// extra space for scroll
@mina_mz你有你的指數倒退。它應該是'System.out.print(arrList.get(i).get(j)' – michaelsnowden
好的修復它。謝謝。 –