【发布时间】:2022-01-03 06:05:48
【问题描述】:
ArrayList<Integer> companiesId = new ArrayList<>();
int numberOfCompanies = 10; // Just for example
...
float profit;
Scanner input = new Scanner(System.in);
for(int i = 0; i < numberOfCompanies; i++) {
int companyId = input.nextInt();
if (!companiesId.contains(companyId)) {
companiesId.add(companyId);
}
if (companiesId.get(i) == 1) {
profit = 1000;
} else if (companiesId.get(i) == 2) {
profit = 2000;
}
}
现在我想打印 ArrayList 中的所有 companyIds,并为每个 id 输入利润,但我不知道如何使用 ArrayList。
应该是:
1 1000
2 2000
...
【问题讨论】:
-
我希望我的答案正是您想要的。
标签: java for-loop arraylist iteration