Luca Natali
Luca Natali

Reputation: 414

Sort an Array of ArrayList<Integer> in java

I want to sort an Array of ArrayList by the first int of the ArrayLists elements. I have tried to override the compare method of the Comparator class but it throws:

Exception in thread "main" java.lang.NullPointerException
at BikeGA$1.compare(BikeGA.java:515)
at BikeGA$1.compare(BikeGA.java:1)
at java.util.TimSort.countRunAndMakeAscending(TimSort.java:351)
at java.util.TimSort.sort(TimSort.java:230)
at java.util.Arrays.sort(Arrays.java:1438)
at BikeGA.main(BikeGA.java:512)

The code is:

int max_generations = 20;
static ArrayList<Integer>[] population = new ArrayList[max_generations];

Arrays.sort(population, new Comparator<ArrayList<Integer>>(){
        @Override
        public int compare(final ArrayList<Integer> entry1, final ArrayList<Integer> entry2){
            return entry1.get(0).compareTo(entry2.get(0));
        }
    });

Can someone help me? Thanks.

Upvotes: 3

Views: 1914

Answers (3)

Mr tintin
Mr tintin

Reputation: 1

In my case object was:

public class Edge {
    private Integer weight;
    public Edge(Integer weight) {
        this.weight = weight;
    }
    public Integer getWeight() {
        return weight;
    }
}

And I call sort for (List edges = new ArrayList<>();) from other class:

Collections.sort(edges,(edge1,edge2) -> edge1.getWeight().compareTo(edge2.getWeight()));

Here is the result: Exception img

Solution was to initialize value of object:

public class Edge {
    private Integer weight = 0;
    public Edge(Integer weight) {
        this.weight = weight;
    }
    public Integer getWeight() {
        return weight;
    }
}

and it work!!! code 0

Upvotes: 0

Eran
Eran

Reputation: 393781

Your Comparator should handle null or empty ArrayLists in order for it to work with any data you put in your array :

    @Override
    public int compare(final ArrayList<Integer> entry1, final ArrayList<Integer> entry2){
        if (entry1 == null && entry2 == null)
            return 0;
        if (entry1 == null)
            return 1;
        if (entry2 == null)
            return -1;
        if (entry1.isEmpty() && entry2.isEmpty())
            return 0;
        if (entry1.isEmpty())
            return 1;
        if (entry2.isEmpty())
            return -1;
        return entry1.get(0).compareTo(entry2.get(0));
    }

This will put the null elements in the end of the array, and the empty lists before them.

Upvotes: 2

Jordi Castilla
Jordi Castilla

Reputation: 26961

You are comparing 2 arrays but you don't check if comparing object exists...

int max_generations = 20;
static ArrayList<Integer>[] population = new ArrayList[max_generations];

Arrays.sort(population, new Comparator<ArrayList<Integer>>(){
        @Override
        public int compare(final ArrayList<Integer> entry1, final ArrayList<Integer> entry2){
            Integer value1 = entry1.get(0) == null : -1 ? entry1.get(0);
            Integer value2 = entry2.get(0) == null : -1 ? entry2.get(0);

            return value1.compareTo(value2);
        }
    });

Upvotes: 0

Related Questions