将增强的for循环转换为for循环

时间:2013-03-14 09:41:03

标签: java

如何将下面的'enhanced for loop'转换为'for loop'

ArrayList<Integer> input= new ArrayList<Integer>();

int[] copies = new int[20];
 for(int allCopies : input) {
        copies[allCopies]++;
    }

我们试过这个:

for(int k = 0; k < input.size(); k++) {

这部分:

for(int allCopies : input) {

但我们不知道如何获得这部分:

copies[allCopies]++;

任何帮助?????

3 个答案:

答案 0 :(得分:2)

ArrayList<Integer> input= new ArrayList<Integer>();

int[] copies = new int[20];
for(int k = 0; k < input.size(); k++) {
    copies[input.get(k)]++;
}

在增强型for循环for(int allCopies : input)

allCopiesinput[counterPosition]相同。

所以基本上所有这一切都是使用k作为计数器来获取该值。

input.get(k)

答案 1 :(得分:2)

这应该这样做:

int allCopies = input.get(k);

答案 2 :(得分:2)

copies[input.get(k)]++;

您应首先查看API以找到合适的方法。