Java 跟踪数组中的数字和计数器数量

Java 跟踪数组中的数字和计数器数量,java,arrays,counter,Java,Arrays,Counter,我需要知道每个人有多少票。我有一个计算,计算有多少候选人在比赛,这是5。现在我有一组arraylist中的数字,我需要计算每个候选对象的数量,候选对象的数量根据我使用的arraylist而变化,但出于测试目的,我使用的test5有5个 //Numbers from arraylist: 1 1 2 5 2 5 5 5 这是我的密码 // My code for (int i=1; i <output.size(); i++){ char checkBallot = ou

我需要知道每个人有多少票。我有一个计算,计算有多少候选人在比赛,这是5。现在我有一组arraylist中的数字,我需要计算每个候选对象的数量,候选对象的数量根据我使用的arraylist而变化,但出于测试目的,我使用的test5有5个

 //Numbers from arraylist:
1
1
2
5
2
5
5
5
这是我的密码

// My code
for (int i=1; i <output.size(); i++){
        char checkBallot = output.get(i).charAt(1); 
        //candidate is array to track how many output as what 
                    // output is array of numbers I want to count. 
        String aString = Character.toString(checkBallot);
        int c = Integer.parseInt(aString);
        int b = c; 
        for (int p= 0; p < count; p++){
            if(c == p+1){
                int oldvalue =0; 
                candidate[c] = c+ oldvalue ;
                oldvalue = c ;
                System.out.println("this is the counter:" +c);
            }

        }
//我的代码

对于(int i=1;i我认为您应该去掉
oldvalue
并使用
++candidate[c];
而不是这三行。假设我了解您的代码,但我几乎不了解。

要计算输出数组列表中有多少1,2,3,4,5:如果您有一个整数数组列表:

final int[] candidate = new int[count];
for(int i : output)
    candidate[i]++;
假设“count”是ArrayList中的最大值

相反,如果您有一个字符串的ArrayList,那么您可以编写

final int[] candidate = new int[count];
for(String i : output)
    candidate[Integer.parseInt(i)]++;

另外,粘贴到代码的其余部分…什么是输出?状态?候选?etcoutput是数组列表中的数字状态是按其顺序排列的,现在的状态是1。候选是一个数组,用于跟踪输出中有多少个1 2 3 4 5。很抱歉,我写我想要实现的东西时很糟糕。@oli我想数一数有多少个1,2,3,4,5 a在输出数组列表中。并将每个数组的数量存储到候选数组中。