我需要打印一个二维数组,其字符串值如表。
到目前为止,我的代码是:
prefix testOnt: <http://www.example.org/test-ontology#>
SELECT ?person ?address
WHERE {
?person testOnt:hasAddress ?address
SERVICE <http://fuseki/location> {
?address ?p ?o # use a more specific BGP here if necessary
}
}
输出结果为:
import java.util.Arrays;
public class Employee {
public static void main(String[] args) {
String wages[][] =
{
{"Emp", "Hours", "Wages"},
{"Bobby", "45", "35"},
{"Rick", "15", "33"},
{"Mike", "66", "50"},
{"Jayme", "15", "45"}
};
System.out.print(Arrays.deepToString(wages));
System.out.println();
}
}
我需要它看起来更像一个包含单独行(5行3列)的表
答案 0 :(得分:4)
您必须迭代数组并打印所有条目。
String wages[][] = {
{"Emp", "Hours", "Wages"},
{"Bobby", "45", "35"},
{"Rick", "15", "33"},
{"Mike", "66", "50"},
{"Jayme", "15", "45"}
};
Arrays.stream(wages).map(Arrays::toString).forEach(System.out::println);
输出
[Emp, Hours, Wages]
[Bobby, 45, 35]
[Rick, 15, 33]
[Mike, 66, 50]
[Jayme, 15, 45]
答案 1 :(得分:1)
一个非常简单的解决方案是这样的:
for (int i = 0; i < wages.length; i++) {
for (int j = 0; j < wages[i].length; j++) {
System.out.print(wages[i][j]);
}
System.out.println();
}
如果字符串的长度导致显示问题,那么您可以执行以下操作:
import java.util.Arrays;
public class Employee {
public int[] getMaxLength(String wages[][]) {
int lengths[] = new int[wages[0].length];
for (int i = 0; i < lengths.length; i++) lengths[i] = 0;
for (int i = 0; i < wages.length; i++) {
for (int j = 0; j < wages[i].length; j++) {
if (wages[i][j].length() > lengths[j]) lengths[j] = wages[i][j].length();
}
}
return lengths;
}
public static String friendlyString(String input, int size) {
while (input.length() < size) input += " ";
return input;
}
public static void main(String[] args) {
String wages[][] =
{
{"Emp", "Hours", "Wages"},
{"Bobby", "45", "35"},
{"Rick", "15", "33"},
{"Mike", "66", "50"},
{"Jayme", "15", "45"}
};
int sizes[] = getMaxLength(wages);
for (int i = 0; i < wages.length; i++) {
for (int j = 0; j < wages[i].length; j++) {
System.out.print(friendlyString(wages[i][j], sizes[j]));
}
System.out.println();
}
}
}