0
我工作的一個項目,並且需要顯示從字符串數組元素對齊的元件,並且兩個水平行的整數數組,如下...從兩個陣列
name1 | name 2 | name3 |
111___ | 2222__ |3333__ |
我遇到問題對齊垂直分隔符。我們應該學習基礎工作,所以我們不應該使用對象。誰能幫忙?
我工作的一個項目,並且需要顯示從字符串數組元素對齊的元件,並且兩個水平行的整數數組,如下...從兩個陣列
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());
}
然後,例如顯示數字時:
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("|");
}
壞消息,Java數組** **是對象。 –