Alisinna
Alisinna

Reputation: 121

Java get last element from List of nodes found within PriorityQueue

I have been having an issue with the following code:

PROBLEM CODE:

Node lastPeek=pq.peek();
tempList3.add(lastPeek.n1.get(n1.size()-1));

where pq is a PriorityQueue (storing objects of type Node (see Node class further down), containing objects of List<Node> and double type). The PriorityQueue is declared in the following way:

Comparator<Node> comparator= new CostComparator();
//creation of priority queue of type Node
PriorityQueue<Node> pq=new PriorityQueue<Node>(comparator);

tempList3 (from the PROBLEM CODE section) is an ArrayList with the following declaration:

List<Node> tempList3=new ArrayList<Node>();

The following is a part of the Node class:

public int dest;
    public Node next;
    public Node parent;
    double cost=0;
    List<Node> n1=new ArrayList<Node>();

    public Node(int d) {
        dest = d;
        next = null;
        parent = null;
    }

     //used for storing objects into PriorityQueue pq
     public Node(List<Node> n, double icost) {
        n1=n; 
        cost=icost;
    }

As seen in the part of the code described as 'PROBLEM CODE', I have been trying to perform peek() operation on the PriorityQueue pq, of type <Node> and storing the value in lastPeek of type Node. The problem arises, when I try to get the last Node within the List value by using .n1.get(n1.size()-1), as n1.size() is not being recognised.

I am getting the error

cannot find symbol- variable n1

in the .get(n1.size()-1) part. My main aim is to get the first element with PriorityQueue pq, and then getting the List part of this first element, which then allows me to get the last Node value within List (this is why I have been trying to use .get(n1.size()-1) ). The elements in PriorityQueue are being stored in the following structure:

Node n=new Node(List<Node> , double);

Upvotes: 0

Views: 775

Answers (1)

Bernd Ebertz
Bernd Ebertz

Reputation: 1327

You'll need to defer n1 from lastPeek both time you're using it like this:

tempList3.add(lastPeek.n1.get(lastPeek.n1.size()-1));

Upvotes: 1

Related Questions