简体   繁体   English

如何在数组中保持运行计数?

[英]How do you keep a running count in an array?

The method is called loadArray() , and this method will load an array with a specified number of random values and update the subscripts in the array to reflect the number of times a random number was generated.该方法称为loadArray() ,该方法将加载具有指定数量的随机值的数组,并更新数组中的下标以反映生成随机数的次数。 The values will be between (0, values.length-1).这些值将介于 (0, values.length-1) 之间。 The signature is public static void loadArray(int[] values, int times) .签名是public static void loadArray(int[] values, int times)

Well, as you commented, essentially you want a function to count how many times a value appears in a array and, after, updates the array (respective element) with the apearing countings.好吧,正如您评论的那样,基本上您想要一个 function 来计算一个值在数组中出现的次数,然后用出现的计数更新数组(各个元素)。

In others words you want to update the values with their respective "frequency" that appears in the array.换句话说,您希望使用数组中出现的它们各自的“频率”来更新这些值。

To to do this I suggest to you a approach using the Map structure.为此,我建议您使用 Map 结构的方法。

How does it work, then?那么它是如何工作的呢?

Excluding the array generation step (thinking only in counting step) we can imagine just put each value of the passed array into a map just checking if the value was previously inserted or not.排除数组生成步骤(仅考虑计数步骤),我们可以想象将传递数组的每个值放入map只是检查该值是否先前插入。

Then, maps are structures that can holds some information while associate that information to a key, this is the known format " key/value ".然后, maps是可以保存一些信息的结构,同时将该信息与键相关联,这就是已知的格式“ key/value ”。

Implementation执行

Ok, to implement this, lets consider a method that generates a array with random numbers, counts it and, after, returns a updated array as we need:好的,为了实现这一点,让我们考虑一个方法,它生成一个带有随机数的数组,对其进行计数,然后根据需要返回一个更新的数组:

public static int[] count(int arrayLength, int rangeOfRandom) {
    //generates the randons
    Random generator = new Random();
    int[] array = new int[arrayLength];
    for (int i = 0; i < array.length; i++) {
        array[i] = generator.nextInt(rangeOfRandom);
    }

    System.out.println("The generated array was: " +
            Arrays.toString(array));

    //counts each value
    HashMap<Integer, Integer> aux = new HashMap<>();
    for (int i = 0; i < array.length; i++) {
        //if the map DOES NOT contains the current array value
        if (!aux.containsKey(array[i])){
            aux.put(array[i], 1); //puts it with "1 counter"
        } else {
            //if not...
            //...overrides the existing value with itself PLUS 1
            aux.put(array[i], aux.get(array[i]) + 1);
        }
    }

    //updates the array
    int[] nArray = new int[array.length];
    for (int key : aux.keySet()){
        for (int i = 0; i < array.length; i++) {
            if (array[i] == key){
                nArray[i] = array[i] + aux.get(key);
            }
        }
    }

    //here we return the updated array
    return nArray;
}

By doing通过做

System.out.println("The result array is: " +
            Arrays.toString(count(5, 10)));

You get a output like this:你会得到一个 output 像这样:

The generated array is: [0, 6, 6, 8, 7]
The result array is: [1, 8, 8, 9, 8]

As you can see, these operations are very elementar, and you can easly refactor it to receive/return other king of parameter and/or types.如您所见,这些操作非常基本,您可以轻松地对其进行重构以接收/返回其他参数和/或类型之王。

For a related discussion, you can check this question .有关相关讨论,您可以查看此问题

Obs.: the method I posted is not optimized, it is only for didatic use. Obs.:我发布的方法没有优化,仅供教学使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java中如何计算数组的元素 - How do you count the elements of an array in java 如何保持Google App Engine的运行实例 - How do you keep a running instance for Google App Engine 如何计算数组中每个元素的出现次数? - How do you count occurrences of each element in an array? 如何递归计算数组(Java)中负数的数量? - How do you recursively count the number of negative numbers in an array (Java)? 使用mod_jk,tomcat和jersey,如何保持长时间运行的请求? - With mod_jk, tomcat, and jersey, how do you keep a long running request alive? 如何在数组中保持不同事物的数量? - How to keep count of different things in an array? 你如何让机器保持清醒? - How do you keep the machine awake? 如何创建代码来计算数组列表中的字符数? (爪哇) - How do you create a code to count the number of characters in an array list? (Java) 您如何从数组列表中提取项目并对其进行计数,以便根据Java中的元素及其总计数创建映射(正确)? - How do you pull items from an array list and count them in order to create a map from the element and its total count (properly) in Java? 你如何计算int中的位数? - how do you count the number of digits in an int?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM