我无法跟踪变量计数增加

时间:2017-12-10 22:15:04

标签: java arrays

我试图得到一个名为frequencyCounters的数组,计算一个数字在一个名为array的数组中重复的次数。索引应该是指数字,即索引0计算0次发生的次数,索引1计算1次发生的次数。它有点工作但不完美。

import java.util.*;

class ArrayTest {
    public static void main(String[] args) {
        int[] array = new int[]{1, 1, 4, 4, 4, 5, 5, 5, 5, 7, 8, 8, 8, 8, 9};

        Arrays.sort(array);
        int max = array[array.length - 1];
        int[] frequencyCounter = new int[max];
        System.out.println("max is " + max);
        int checkNumber = 1;
        int index = 0;
        int length = array.length - 1;
        for (int i = 0; i <= 8; i++) {
            System.out.println("i is " + i);
            if (index == 0) {
                System.out.println("running index=0 statement");
                while (checkNumber == array[index]) {

                    if (i == 0) {
                        System.out.println("Running i==0 statement");
                        frequencyCounter[i] = frequencyCounter[i] + 1;
                    } else if (i != 0) {
                        System.out.println("Running i!=0 statement");
                        frequencyCounter[i - 1] = frequencyCounter[i - 1] + 1;
                    }

                    index = index + 1;


                }
                checkNumber = checkNumber + 1;
            } else {

                System.out.println("index is " + index);
                System.out.println("running i=0 else statement");

                while (checkNumber == array[index]) {

                    if (i == 0) {
                        System.out.println("Running i==0 statement");
                        frequencyCounter[i] = frequencyCounter[i] + 1;
                    } else {
                        System.out.println("Running i!=0 statement");
                        frequencyCounter[i - 1] = frequencyCounter[i - 1] + 1;
                    }

                    index = index + 1;
                }
                checkNumber = checkNumber + 1;
            }
            System.out.println("index is " + index + " at end of loop for");
        }
        int output = 1;
        for (int Z = 0; Z <= 8; Z++) {


            System.out.printf("The number %d repeats %d  


                    times",output,frequencyCounter[Z]             
                    output++;
        }
    }


}

由于某种原因,索引突然从输出语句中的5跳到9然后再跳到14。我是一名高中学生,并希望在这个问题上得到一些帮助:)我需要找到阵列的模式。我不允许使用像hashmaps这样的花哨的东西。我只能通过简单的操作使用数组类和循环。

2 个答案:

答案 0 :(得分:1)

你可以创建一个在原始数组中具有最大int(+1)大小的数组,循环,计算每个int的出现次数并将计数放在索引上。

public static void main(String[] args) {
    int [] array=new int[] {1,1,4,4,4,5,5,5,5,7,8,8,8,8,9};
    // replace the next line with sth else to get the max of the array.
    int max = Arrays.stream(array).max().getAsInt();
    int[] res = new int[max + 1];
    for (int i = 0; i < res.length; i++) {
        res[i] = 0;
        for (int anArray : array) {
            res[i] += anArray == i ? 1 : 0;
        }
    }
    /* for (int re : res) {
        System.out.println(re);
    } */
}

答案 1 :(得分:0)

当你对阵列进行排序时,你已经在那里了一半。你有没有想过像

这样的东西
// sort your array as you've done

int previousElement = array[0];
int curCount = 1;
int[] tmp;
int[] result;

for (int i = 1; i < array.length; i++) {
    curElement = array[i];
    if (curElement == previousElement) {
        curCount++;
    } else {
        // curCount will be the index and curElement its value
        // look into using tmp and result to get result[curCount] = curElement for all the indices necessary
        curCount = 1;
    }
}

另见Make copy of array Java