-2

This is the ListNode class:

  public class ListNode {
      int val;
      ListNode next;
      ListNode(int x) { val = x; }
  }

I am trying to initialize a PriorityQueue like this:

PriorityQueue<ListNode> heap = new PriorityQueue(lists.length,
            (l1, l2) -> l1.val < l2.val ? -1 :
                        l1.val == l2.val ? 0 :
                        1);

But I am getting "cannot find symbol: variable val". What is the proper way to do this? I've tried casting l1 and l2 as ListNode's, but this does not do anything.

Edit: Why does this work?

    PriorityQueue<ListNode> queue= new PriorityQueue<ListNode>(lists.size(),new Comparator<ListNode>(){
            @Override
            public int compare(ListNode o1,ListNode o2){
                if (o1.val<o2.val)
                    return -1;
                else if (o1.val==o2.val)
                    return 0;
                else 
                    return 1;
            }
        });
DChalo123
  • 165
  • 3
  • 11

4 Answers4

1

You are missing the diamond operator on the right hand side which made the compiler not infer the argument type as ListNode (It inferred it as an Object and hence you cannot access val)

Try this

 PriorityQueue<ListNode> heap = new PriorityQueue<>(lists.length,
            (l1, l2) -> l1.val < l2.val ? -1 :
                    l1.val == l2.val ? 0 :
                            1);

My initial answer:

If you have your PriorityQueue declaration in a different package (from the one where ListNode is declared):

It has nothing to do with the ternary opeator. The variable val is package-private. Making it public will solve the issue but it is not a good practice. Instead, add a getter for it and make the instance variables private (unless you have a reason for making them package-private)

public int getVal() {
    return val;
}


PriorityQueue<ListNode> heap = new PriorityQueue<>(lists.length,
        (l1, l2) -> l1.getVal() < l2.getVal() ? -1 :
                    l1.getVal() == l2.getVal() ? 0 :
                    1);
Thiyagu
  • 17,362
  • 5
  • 42
  • 79
  • But I can access them outside of the Ternary operators. I can create a ListNode object and access it's variables outside it. – DChalo123 Sep 28 '18 at 03:32
  • 1
    Most probably, you have tried to access val from the *same package* or namespace and the `PriorityQueue` declaration is in a different package – Thiyagu Sep 28 '18 at 03:33
  • Thanks. I'm doing this on LeetCode, so I don't have access to that stuff. Also thanks to whoever downvoted me. This is why I hate asking questions here. – DChalo123 Sep 28 '18 at 03:34
  • @JohnTravicks I did not downvote btw. People are expected to do some research before asking a question here. You would have got this answer if you had tried searching for the error. But I can understand that you got misled thinking it is due to the ternary operator. – Thiyagu Sep 28 '18 at 03:36
  • Can you explain why the solution in my edit works? What's different about how that is accessing val compared to the ternary operator? – DChalo123 Sep 28 '18 at 03:39
  • @JohnTravicks See my **EDIT** – Thiyagu Sep 28 '18 at 03:44
  • @JohnTravicks You now have the params as `ListNode`. Earlier they were `Object`. Any good IDE would have shown you a warning when you had `new PriorityQueue(...)` instead of `new PriorityQueue<>(..)` – Thiyagu Sep 28 '18 at 03:48
  • Yeah. I just realized that. – DChalo123 Sep 28 '18 at 03:49
1

change int val to public int val

or make a getter method and use it

    public class ListNode {
         public int val;
         ListNode next;
         ListNode(int x) { val = x; }
     }
DevMS
  • 72
  • 5
1

@user7 has a correct answer, but I would suggest a different best practice here:

Implement the interface Comparable<ListNode> on ListNode and put the comparing function in the ListNode class implementation. Then just use

new PriorityQueue<ListNode>()

And it will use the „natural order“ of elements

iPirat
  • 2,197
  • 1
  • 17
  • 30
  • Thanks. Turns out my problem was a syntax error. I forgot to add the <> before the parenthesis – DChalo123 Sep 28 '18 at 03:48
  • Forgetting <> is not a syntax error. It helps Compiler understand what you try to do. But yes, using genetics will help compiler (and it will help the developer) :). Using Comparable in such a scenario. Is still the preferred way: this way ListNode knows how priorities itself against other ListNodes. Not your “outside program” which creates the queue. – iPirat Sep 28 '18 at 03:51
  • PS: added generics in my answer as well :) – iPirat Sep 28 '18 at 03:53
0

I found the answer. It was a syntax error.

PriorityQueue<ListNode> heap = new PriorityQueue<>(lists.length,
            (l1, l2) -> l1.val < l2.val ? -1 :
                        l1.val == l2.val ? 0 :
                        1);

This works.

DChalo123
  • 165
  • 3
  • 11