[英]Sorting an Integer type Array in java
Cannot use Arrays.sort()
for Integer
type array if it has null elements.如果具有 null 元素,则不能将Arrays.sort()
用于Integer
类型数组。 Unlike int
type array, null
values are not assigned to 0.与int
类型数组不同, null
的值未分配为 0。
How to sort the Integer
type array if it has a null
element?如果Integer
类型数组具有null
元素,如何对其进行排序?
Integer[] arr=new Integer[4] {6,7,null,null};
I want to sort this arr
array variable.我想对这个arr
数组变量进行排序。
The error is as follows:错误如下:
Exception in thread "main" java.lang.NullPointerException: Cannot invoke "java.lang.Comparable.compareTo(Object)" because "a[runHi]" is null
at java.base/java.util.ComparableTimSort.countRunAndMakeAscending(ComparableTimSort.java:320)
at java.base/java.util.ComparableTimSort.sort(ComparableTimSort.java:188)
at java.base/java.util.Arrays.sort(Arrays.java:1040)
at Example.medicinePriceForGivenDisease(Example.java:41)
at Example.main(Example.java:21)
Since Integer
is a Comparable
, you can use the overloaded Arrays.sort(Object[])
.由于Integer
是Comparable
,因此您可以使用重载的Arrays.sort(Object[])
。 Ie: IE:
Integer[] arr = new Integer[4]; //not "int"
// fill in some data in arr
Arrays.sort(arr);
The java.util.Arrays
class has all required methods for sorting, including sort(Object[] a)
. java.util.Arrays
class 具有排序所需的所有方法,包括sort(Object[] a)
。 Since Integer
is an object, it will do nicely:由于Integer
是 object,它会做得很好:
Integer[] arr = new Integer[] { 32, 12, 9, 77 };
Arrays.sort(arr);
// Array sorted it
The single parameter Arrays.sort(Object[])
sorts elements that must implement the Comparable
interface using the compareTo
method.单个参数Arrays.sort(Object[])
使用compareTo
方法对必须实现Comparable
接口的元素进行排序。 Basically it calls e1.compareTo(e2)
for elements of the array.基本上它为数组的元素调用e1.compareTo(e2)
。 This will throw an Exception if either e1
or e2
is null.如果e1
或e2
是 null,这将引发异常。
Solution: use the 2-parameter Arrays.sort(T[], Comparator<? super t)
and provide a Comparator
that is able to compare 2 elements even if one or both are null.解决方案:使用 2 参数Arrays.sort(T[], Comparator<? super t)
并提供一个能够比较 2 个元素的Comparator
器,即使其中一个或两个是 null。
Very simple example (compares null
as being 0
):非常简单的例子(比较null
为0
):
var comparator = Comparator.comparingInt((Integer n) -> n==null ? 0 : n.intValue());
Arrays.sort(arr, comparator);
A class implementing Comparator
can also be used for more complex ordering:实现Comparator
器的 class 也可用于更复杂的排序:
class MyComparator implements Comparator<Integer> {
@Overwrite
public int compare(Integer a, Integer b) {
if (a == null && b == 0) return 0;
if (a == null) return -1;
... // must be implemented
}
}
Arrays.sort(arr, new MyComparator());
no IDE was used or hurt while composing this post;在撰写这篇文章时没有使用或伤害 IDE; included code is meant only as idea how to solve the problem - it is incomplete, eventually wrong or even contain compilation errors包含的代码仅作为如何解决问题的想法 - 它不完整,最终错误甚至包含编译错误
List<Integer> sortedArray = Arrays.stream(arr)
.filter(x -> x != null)
.sorted().toList();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.