我有一个带有三个String字段的Variation类:id,name,price。我用随机字符串,“名称”加索引(用于名称)和“价格” +索引(用于价格)的随机字符串填充了其中的25个列表。
当我遍历每个变体时,我正在使用:System.out.println(i + "\tid: " + variation.id + "\tname: " + variation.name + "\tprice: " + variation.price);
我的输出是:
I/System.out: 0 id: 6N8D06S80DIEJQH9KJAG name: name0 price: price0
I/System.out: 1 id: XCKGYA0W4YNKE25MRGKD name: name1 price: price1
I/System.out: 2 id: UGI8Y6MCNDKEGR74F13L name: name2 price: price2
I/System.out: 3 id: EHLXW3FGYSNE2XMFCT1P name: name3 price: price3
I/System.out: 4 id: X1ONX4WZLWH41Z7UW3TE name: name4 price: price4
I/System.out: 5 id: VG65ZGTDQ5C2TBYF26MZ name: name5 price: price5
I/System.out: 6 id: HGK12SKUXSOZK0JDLL70 name: name6 price: price6
I/System.out: 7 id: DQJTDT108CWBJQOED06X name: name7 price: price7
I/System.out: 8 id: YTRIZIMS5N5VZQUEJQ8L name: name8 price: price8
I/System.out: 9 id: 59DGBOFULRZPJF7P53PN name: name9 price: price9
I/System.out: 10 id: BLQ78XTDZQBUIXICWF2Q name: name10 price: price10
I/System.out: 11 id: NY3PZ5RRIWAF92O5Z49D name: name11 price: price11
I/System.out: 12 id: KPLYK1GFBIL92JPSYNK8 name: name12 price: price12
I/System.out: 13 id: 6MTB849NN6IZ2I5GSTF5 name: name13 price: price13
I/System.out: 14 id: 7718U4CXMD97YY1TUQ38 name: name14 price: price14
I/System.out: 15 id: GJOUN8U0SVIZDIH2WHAY name: name15 price: price15
I/System.out: 16 id: D3CTCP9E7722LCRF07SM name: name16 price: price16
I/System.out: 17 id: IWHDK9N49YC3DJOK0JY0 name: name17 price: price17
I/System.out: 18 id: 0ZTGE33GDWG42QGLDTHJ name: name18 price: price18
I/System.out: 19 id: GRRJQ2KFLNFPS817WQCE name: name19 price: price19
I/System.out: 20 id: GJSHJMLYXMMB5ZKDJ91B name: name20 price: price20
I/System.out: 21 id: 2DE7RW6M50N3M7HE7IJG name: name21 price: price21
I/System.out: 22 id: HBR2SVAHWXUP8QDQONIZ name: name22 price: price22
I/System.out: 23 id: OAYUHH3138Q6PTRJX7HB name: name23 price: price23
I/System.out: 24 id: FFIK3FR3K7ZKOYV77E2O name: name24 price: price24
以下是代码的相关部分:
主要:
List<Variation> variationsList = new ArrayList<>();
void populateVariationsList() {
for(int i = 0 ; i < 25; i++)
variationsList.add(new Variation(getRandomId(), "name" + i, "price" + i));
}
String getRandomId() {
String CHARS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890";
StringBuilder randomId = new StringBuilder();
Random rnd = new Random();
while (randomId.length() < 20)
randomId.append(CHARS.charAt(rnd.nextInt(36)));
return randomId.toString();
}
public void onResume() {
super.onResume();
for(int i = 0; i < variationsList.size(); i++) {
Variation variation = variationsList.get(i);
System.out.println(i + "\tid: " + variation.id + "\tname: " + variation.name + "\tprice: " + variation.price);
}
}
Variation.java:
public class Variation {
String id;
String name;
String price;
public Variation(String id, String name, String price) {
this.id = id;
this.name = name;
this.price = price;
}
}
它不是那么重要,我可能忽略了一些愚蠢的事情,但是谁能看到为什么它不打印所有标签?
最佳答案
正如其他人所说的那样,这是预期的行为。但是,如果您想要统一的输出(对此我很强迫,我会在onResume方法的for循环中添加一个if else语句,该语句在每个单个数字前添加一个0,这将导致制表符全部统一。
public void onResume() {
super.onResume();
for(int i = 0; i < variationsList.size(); i++) {
Variation variation = variationsList.get(i);
if(i < 10){
System.out.println( "0" + i + "\tid: " + variation.id + "\tname: " + variation.name +
"\tprice: " + variation.price);}
else{
System.out.println(i + "\tid: " + variation.id + "\tname: " + variation.name + "\tprice: " + variation.price);
}}}