Priority Queue of an array of integers in java
Asked Answered
O

6

8

I want to compare the second element of the following array:

int[][] intervals = new int[][]{new int[]{0, 30},new int[]{5, 10},new int[]{15, 20}};

My priority queue with custom comparator:

PriorityQueue<int[]> heap = new PriorityQueue(intervals.length, (a, b) -> a[1] - b[1]);

But I get the below 2 errors:

Line 8: error: array required, but Object found
        PriorityQueue<Integer[]> heap = new PriorityQueue(intervals.length, (a, b) -> a[1] - b[1]);
                                                                                       ^
Line 8: error: array required, but Object found
        PriorityQueue<Integer[]> heap = new PriorityQueue(intervals.length, (a, b) -> a[1] - b[1]);
                                                                                             
Oriole answered 6/7, 2020 at 12:7 Comment(2)
Does this answer your question? What is a raw type and why shouldn't we use it?Tula
In short, you shouldn't use a raw type here. new PriorityQueue<>(intervals.length, (a, b) -> a[1] - b[1]) worksTula
F
6

You could use Integer.compare

PriorityQueue<int[]> heap = new PriorityQueue<>(intervals.length, (a,b) -> Integer.compare(a[1],b[1]));
Flump answered 2/4, 2021 at 19:23 Comment(0)
W
6

You should cast a to int array first.

PriorityQueue<int[]> heap = new PriorityQueue(intervals.length, (a, b) -> ((int[])a)[1] - ((int[])b)[1]);
Wyattwyche answered 7/7, 2020 at 6:11 Comment(1)
This answer is incorrect. See stackoverflow.com/questions/2728793Golem
F
6

You could use Integer.compare

PriorityQueue<int[]> heap = new PriorityQueue<>(intervals.length, (a,b) -> Integer.compare(a[1],b[1]));
Flump answered 2/4, 2021 at 19:23 Comment(0)
M
4

This is because you are using a parameterized class rawly. Add <> after PriorityQueue:

PriorityQueue<int[]> heap = new PriorityQueue<>(intervals.length, (a, b) -> a[1] - b[1]);
Mirtamirth answered 7/3, 2022 at 8:44 Comment(1)
This answer is incorrect. See stackoverflow.com/questions/2728793Golem
N
3

Well, in your example, you have a 2D array; however, in your code snippet, you're assuming that a single-dimensional array should be kept in the Queue. So, which is your question? are you sure you want to maintain the Queue of 2D arrays? that doesn't mean, that you would have a Queue of pairs/tuples, that means, that each element in that queue, will be a discrete 2D array object. And in this case, probably each array will have more than one element.. and that means, that you should most likely iterate through them.. but if you're sure what you want with this code to be achieved and it's correct, then you can compare first elements like this:

    PriorityQueue<int[][]> heap = new PriorityQueue<int[][]>((a, b) -> {
        if (a[0][0] > b[0][0]) {
            return 1; //change according to your logic
        } else if (a[0][0] < b[0][0]) {
            return -1; //change according to your logic
        } else {
            return 0; //change according to your logic
        }
    });
Nunciata answered 6/7, 2020 at 12:19 Comment(1)
Well yes ... but (a, b) -> Integer.compare(a[0][0], b[0][0]) would be more concise.Golem
M
3

You can use:

PriorityQueue<int[]> pq = new PriorityQueue<>(intervals.length,
                             Comparator.comparingInt(interval -> interval[1]));
Masseter answered 4/6, 2021 at 11:8 Comment(0)
I
2

Your code is missing one vital syntax. You can add <> or <int[]> to the RHS of the assignment statement and it will work. So the line statement will become:

PriorityQueue<int[]> heap = new PriorityQueue<int[]>(intervals.length, (a, b) -> a[1] - b[1]);
Irs answered 8/7, 2023 at 0:3 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.