简体   繁体   English

Java从PriorityQueue中找到的节点列表中获取最后一个元素

[英]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). 其中pq是PriorityQueue(存储Node类型的对象(请参见下面的Node类),包含List<Node>和double类型的对象)。 The PriorityQueue is declared in the following way: PriorityQueue通过以下方式声明:

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: tempList3(来自“问题代码”部分)是具有以下声明的ArrayList:

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

The following is a part of the Node class: 以下是Node类的一部分:

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. 从描述为“问题代码”的代码部分可以看出,我一直在尝试对<Node>类型的PriorityQueue pq执行peek()操作,并将值存储在Node类型的lastPeek中。 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. 当我尝试通过使用.n1.get(n1.size()-1)获取List值内的最后一个Node时出现问题,因为未识别n1.size()。

I am getting the error 我收到错误

cannot find symbol- variable n1 找不到符号变量n1

in the .get(n1.size()-1) part. 在.get(n1.size()-1)部分中。 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) ). 我的主要目的是使用PriorityQueue pq获取第一个元素,然后获取该第一个元素的List部分,然后使我能够获取List中的最后一个Node值(这就是为什么我一直尝试使用.get(n1 .size()-1))。 The elements in PriorityQueue are being stored in the following structure: PriorityQueue中的元素以以下结构存储:

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

每次使用时都需要从lastPeek延迟n1,如下所示:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM