【发布时间】:2020-07-16 12:48:39
【问题描述】:
我想创建一个包含每个字母 5 次的字母列表。我尝试了一个代码,它成功了,
public class AlphabetsTest {
public static void main(String[] args) {
List<Character> alphabetList = new ArrayList<>();
for (int i=0; i<3; i++){
char chr='a';
if (i==1)
chr = 'b';
if (i==2)
chr = 'c';
for (int j=0; j<5; j++){
alphabetList.add(chr);
}
}
}
}
但我必须为更多字母添加多个 if 条件。有没有更好的方法来避免它。
【问题讨论】: