如何排序包含null元素对象的数组? [英] How to sort an array of objects containing null elements?

查看:832
本文介绍了如何排序包含null元素对象的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的节目一个数组 fClasses 固定长度的创建[7]的对象,每个对象是类 FCLASS 包含3 字符串,一个 INT ,以及 INT [] 。这些值从.txt文件中读取并添加到阵列中的一个特定的指数基础上的 INT 的值。有在.txt文件少的条目然后有数组索引所以数组最后应该是这样的:

In my program an array fClasses of fixed length [7] of objects is created, each object is a class FClass that contains 3 Strings, an int, and an int[]. These values are read from a .txt file and added to a specific index of the array based on the value of the int. There are less entries in the .txt file then there are indices in the array so the array ends up looking something like this:

fClasses[0] { str1, str2, str3, int1, int [] {1,2,3,4,5}}
fClasses[1] { str1, str2, str3, int1, int [] {1,2,3,4,5}}
fClasses[2] { str1, str2, str3, int1, int [] {1,2,3,4,5}}
fClasses[3] null
fClasses[4] null
fClasses[5] { str1, str2, str3, int1, int [] {1,2,3,4,5}}
fClasses[6] { str1, str2, str3, int1, int [] {1,2,3,4,5}}

在稍后的节目,我需要根据在 INT [] 整数的数组进行排序>。我有一个工作方法返回此但当我尝试使用数组排序的compareTo Arrays.sort 我得到开始这些错误的一个长长的清单:

Later in the program I need to sort the array based on the average of the ints in the int[]. I have a working method to return this but when I try to sort the array using compareTo and Arrays.sort I get a long list of errors starting with these:

Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    at java.util.ComparableTimSort.countRunAndMakeAscending(Unknown Source)
    at java.util.ComparableTimSort.sort(Unknown Source)
    at java.util.Arrays.sort(Unknown Source)
    at FProg.sortClasses(FProg.java:228)

我的的compareTo 方法看起来是这样的,它的位置在一个类中实现可比

My compareTo method looks like this and it's located in a class that implements Comparable:

public int compareTo(FClass other) 
{
    if (other == null || this.avg == other.avg)
    {
        return 0;
    }
    else if (this.avg < other.avg)
    {
        return -1;
    }
    else
    {
        return 1;
    }

}

和我想调用这个方法来进行排序:

And I'm trying to call this method to do the sorting:

public void sortClasses()
{
    Arrays.sort(fClasses, 0, MAX_CLASSES);
}

我已经与包含足够的条目,以填补阵列和排序工作正常在这种情况下.txt文件进行测试,所以我相信我遇到的问题是,我的排序方法不能进行排序一个数组在它null元素。有没有这种可以实现什么办法?

I have tested it with a .txt file that contains enough entries to fill the array and the sort works correctly in that case, so I believe the problem I'm having is that my sort method can't sort an array with null elements in it. Is there any way this can be achieved?

推荐答案

您需要用自己的比较执行情况,并检查空值,返回0

You need your own Comparator implementation and check for nulls and return 0

 Arrays.sort(fClasses, new Comparator<FClass>() {
    @Override
    public int compare(FClass o1, FClass o2) {
        if (o1 == null && o2 == null) {
            return 0;
        }
        if (o1 == null) {
            return 1;
        }
        if (o2 == null) {
            return -1;
        }
        return o1.compareTo(o2);
    }});

这篇关于如何排序包含null元素对象的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆