我有两个ArrayLists,如:
infromation,id,key3,something,... - >这是array1
some_value,1539,value3,nothing,... - >这是array2
我想加入key1和value1,key2和value2等,如下例所示:
information...........some_value
id....................1539
key3..................value3
something.............anything
我尝试使用此代码:
Integer dotsNeeded = 50 - array1.length();
String repeated = new String(new char[dotsNeeded]).replace("\0", ".");
finaArray.add( array1.get(i) + repeated + array2.get(i) );
但结果并不好,因为不同的字母需要不同的空间。
答案 0 :(得分:0)
试试这个
ArrayList<String> list1;
ArrayList<String> list2;
ArrayList<String> formatted = new ArrayList<>();
int longest;
for (String str : list1) {
if (longest < str.length()) longest = str.length();
}
longest += 10;
for (int i = 0; i < list1.size(); i++) {
String item1 = list1.get(i);
String periods = new String(new char[longest-item1.length()]).replace("\0", ".");
formatted.add(item1 + periods + list2.get(i));
}
它找到第一个列表中最长的项目,然后添加longest += 10
的额外句点。然后将相同索引的项与它们之间的句点连接起来。
答案 1 :(得分:0)
这样的事情:
StringBuilder builder = new StringBuilder();
int lineSize = 20;
for (int i=0 ; i<key.size() ; i++){
builder.append(key.get(i));
builder.append(new String(new char[lineSize-key.get(i).length()]).replace("\0", "."));
builder.append(value.get(i));
builder.append("\n");
}
Log.d(TAG, builder.toString());