1

can we define a Priority Queue of an array that compare arrays by their length?

    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());
    }
}

output :
[I@1c20c684
[I@1fb3ebeb
[I@548c4f57

roeygol
  • 4,908
  • 9
  • 51
  • 88
Mina Shaigan
  • 47
  • 2
  • 5

1 Answers1

1

You can, with whatever comparator you want. Your code is printing addresses of the array as output. You can use java.util.Arrays.toString() method.

Jared Burrows
  • 54,294
  • 25
  • 151
  • 185
Abhishek Kumar
  • 292
  • 3
  • 10