[英]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 结构的方法。
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
”。
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.