Java 从两个阵列对齐元素
我正在处理一个项目,需要在两个水平行中显示字符串数组和整数数组中的元素,如下所示Java 从两个阵列对齐元素,java,arrays,Java,Arrays,我正在处理一个项目,需要在两个水平行中显示字符串数组和整数数组中的元素,如下所示 name1 | name 2 | name3 | 111___ | 2222__ |3333__ | 对齐垂直分离器时遇到问题。我们应该学习基础工作,所以我们不应该使用对象。有人能帮忙吗 显示每个数组的元素时,根据所需单元格的宽度用空格填充。此宽度可以创建为第三个数组,也可以就地创建为字符串宽度和整数宽度之间的最大值 int[] cellWidth = new int[yourIntegerArray.
name1 | name 2 | name3 |
111___ | 2222__ |3333__ |
对齐垂直分离器时遇到问题。我们应该学习基础工作,所以我们不应该使用对象。有人能帮忙吗 显示每个数组的元素时,根据所需单元格的宽度用空格填充。此宽度可以创建为第三个数组,也可以就地创建为字符串宽度和整数宽度之间的最大值
int[] cellWidth = new int[yourIntegerArray.length];
for (int index = 0; index < cellWidth.length; index++) {
Integer i = yourIntegerArray[index];
String s = yourStringArray[index];
cellWidth[index] = Math.max(s.length(), i.toString().length());
}
int[]cellWidth=newint[yourIntegerArray.length];
for(int index=0;index
然后,在显示数字时,例如:
for (int index = 0; index < yourIntegerArray.length; index++) {
Integer i = yourIntegerArray[index];
// print the number
System.out.print(i);
// add the spaces
int nbSpacesToAdd = cellWidth[index] - i.toString().length;
printSpaces(nbSpacesToAdd); // you should write that one easily
// print the separator
System.out.print("|");
}
for(int index=0;index
坏消息,Java数组是对象。