列表中的Java频率
问题在于nullpointexception。当我输入例如:1 4-一切都很好,因为输入的大小与数组大小相同。但如果我输入14-我会得到异常,并且不知道如何修复它。也许有人对此有什么想法?Integer maxSize=Collections.maxarlist 返回集合中的最大元素 使用整数maxSize=arrList.size列表中的Java频率,java,Java,问题在于nullpointexception。当我输入例如:1 4-一切都很好,因为输入的大小与数组大小相同。但如果我输入14-我会得到异常,并且不知道如何修复它。也许有人对此有什么想法?Integer maxSize=Collections.maxarlist 返回集合中的最大元素 使用整数maxSize=arrList.size 而不是整数maxSize=Collections.maxarlist 从技术上讲,您可以使用集合而不是数组,并在其上执行Collection.frequencyCo
而不是整数maxSize=Collections.maxarlist 从技术上讲,您可以使用集合而不是数组,并在其上执行Collection.frequencyCollection,Object 返回指定集合中等于的元素数 指定的对象。更正式地说,返回元素e的数量 在集合中,使Objects.equal 您必须使用Map保存数字->频率数据。您可以使用流按数据分组
Map<Integer, Long> counted = list.stream()
.collect(Collectors.groupingBy(Function.identity(), Collectors.counting()));
我假设这是一种赋值,所以您应该在不使用高级预构建java算法的情况下解决这个问题。由于你试图自己解决这个问题,我愿意帮忙。您的主要问题与算法本身无关,但与装箱/取消装箱相关的一些Java特性有关:您试图访问的数组元素为null,在这种情况下,转换为整数取消装箱将不起作用,并引发NullPointerException 与其解决NullPointerException问题,我建议从一开始就检查分配: 如果我们假设我们已经知道输入数字的最小值和最大值,那么计算所有输入数字的频率就很容易了:
/**
* @param input the input numbers
* @param min the minimum input number (min(input))
* @param max the maximum input number (max(input))
* @return the frequency of all input numbers as array. The n-th array element denotes the frequence of the number n+min.
*/
int[] frequency(int[] input, int min, int max) {
int[] result = new int[max - min + 1]; // e.g. int[4] for min = 2 and max = 5
for(int i = 0; i < input.length; ++i) { // iterate all input numbers
int n = input[i]; // the number
int index = n - min; // the index in the frequency array
result[index]++;
}
return result;
}
最小值和最大值可以很容易地计算如下:
/**
* Computes the boundaries (minimum and maximum) of the given number array
* @param input the input numbers
* @return the minimum and maximum number of the input numbers as array of length 2. The first element is the minimum, the second element the maximum value.
*/
int[] boundaries(int[] input) {
int[] result = new int[2]; // result[0] = min, result[1] = max
int min = Integer.MAX_VALUE;
int max = Integer.MIN_VALUE;
for(int i = 0; i < input.length; ++i) {
int n = input[i];
min = Math.min(min, n);
max = Math.max(max, n);
}
return new int[] { min, max };
}
剩下的很简单:
/**
* Computes the frequencies of the given input numbers.
* @returns the frequencies of the input numbers as two-dimensional array. The first element of the array is an array of the frequencies, the second element the array of numbers.
*/
int[][] frequencies(int[] input) {
// get min/max of input
int[] minMax = boundaries(input);
int min = minMax[0];
int max = minMax[1];
// compute frequencies
int[] frequencies = frequency(input, min, max);
// create numbers array
int[] numbers = new int[frequencies.length]);
for(int i = 0; < frequency.length; ++i) {
numbers[i] = min + i;
}
// assembly two dimensional result array
return new int[][] { frequencies, numbers };
}
所有这些都可以通过使用java运行时环境的内置机制(例如,流式API)来计算最小/最大值来简化。为什么不使用映射,这要容易得多。Collections.maxarlist返回列表中的最大元素。不是列表的长度。因此,使用新的整数[maxSize]没有意义。您的方法需要整数的原始列表作为输入,并且必须为其输出创建另一个列表或数组。不能对这两个数组使用相同的数组。哪一行导致了异常?@isnot2bad如果arr[elementIndex]>0是,它将返回max element,这是有意义的。因为我需要循环所有元素直到最大元素,而不是通过输入大小。您可以在示例中看到我的解释:示例:A=[1,4,1,5,8,1,3,5,1,4,1,3,7,2]输出:频率:5,1,2,2,2,0,1,1数字:1,2,3,4,5,6,7,8输入1,4导致创建大小为4整数的数组[]A=arrList.toArraynew Integer[maxSize];但是由于您有3个元素,A看起来像是在A[0]=1A[1]=1A[2]=4A[3]=null下面,这导致您为nullpointer@Ntel如果循环到max元素,那么示例中的max元素是8,所以当达到8时,应该停止循环,因此3的频率应该是0,因为数组中8之前没有3。您应该循环遍历所有元素,直到max元素,并计算它们各自的频率,然后将其存储在单独的数组中。这不包括列表中没有的中间数字,但添加这些数字可以在第二步中轻松完成。我将尝试使用Map