String twoD[][] = new String[2][2];
twoD[0][0] = "Hi";
twoD[1][0] = "Hi";
twoD[1][1] = "What's";
twoD[2][0] = "Hi";
twoD[2][1] = "What's";
twoD[2][2] = "Up?";
for (int row = 0; row < twoD.length; row++ ){
for(int col = 0; col < twoD[row].length; col++){
System.out.println(twoD[row][col] + "\t");
}
System.out.println();
}
你好
嗨什么是
嗨,怎么了?
您好 空值 空
您好 什么是 空
您好 什么是 吗?
答案 0 :(得分:0)
我想你想创建一个锯齿状的数组,你可以这样做
Error occurred: Error
Domain=NSURLErrorDomain Code=-1002 "unsupported URL"
UserInfo={NSUnderlyingError=0x1546a36b0{Error Domain=kCFErrorDomainCFNetwork Code=-1002 "(null)"}, NSErrorFailingURLStringKey=(null)https://(api-path),...}
然后你可能想在你的循环中String[][] twoD = { { "Hi" }, { "Hi", "What's" }, { "Hi", "What's", "Up" } };
(而不是print
)
println
或者,使用现有代码,您可以在for (int row = 0; row < twoD.length; row++) {
for (int col = 0; col < twoD[row].length; col++) {
System.out.print(twoD[row][col] + " ");
}
System.out.println();
}
之前添加null
支票
print
答案 1 :(得分:0)
另一个答案是使用顺序流。
String twoD[][] = new String[3][3]; // not [2][2]
twoD[0][0] = "Hi";
twoD[1][0] = "Hi";
twoD[1][1] = "What's";
twoD[2][0] = "Hi";
twoD[2][1] = "What's";
twoD[2][2] = "Up?";
System.out.println(Arrays.stream(twoD)
.map(oneD -> Arrays.stream(oneD)
.filter(Objects::nonNull)
.collect(Collectors.joining(" ")))
.collect(Collectors.joining("\n")));