在java中,我们可以有一个整数数组的优先级队列吗

在java中,我们可以有一个整数数组的优先级队列吗,java,priority-queue,Java,Priority Queue,我们可以定义一个数组的优先级队列来比较数组的长度吗 PriorityQueue<int[]> pq=new PriorityQueue<int[]>(5, (a,b) -> a.length - b.length); int[] a = new int[]{1,2}; int[] b = {1,2,3}; int[] c = new int[]{3}; pq.add(a); pq.add(b); pq.add(

我们可以定义一个数组的优先级队列来比较数组的长度吗

    PriorityQueue<int[]> pq=new PriorityQueue<int[]>(5, (a,b) -> a.length - b.length);
    int[] a = new int[]{1,2};
    int[] b = {1,2,3};
    int[] c = new int[]{3};
    pq.add(a);
    pq.add(b);
    pq.add(c);
    while (pq.size() != 0)
    {
        System.out.println(pq.remove());
    }
}
PriorityQueue pq=新的PriorityQueue(5,(a,b)->a.length-b.length;
int[]a=新的int[]{1,2};
int[]b={1,2,3};
int[]c=新的int[]{3};
pq.增加(a);
pq.增加(b);
pq.添加(c);
而(pq.size()!=0)
{
System.out.println(pq.remove());
}
}
输出:
[I@1c20c684
[I@1fb3ebeb

[I@548c4f57

您可以使用任何您想要的比较器。您的代码正在将数组的地址打印为输出。您可以使用
java.util.Arrays.toString()
方法。

尝试
System.out.println(Arrays.toString(pq.remove());
但是如果我们想将它放在另一个数组中呢?