I am writing a program to calculate the shortest path between two nodes. In my program I have one custom class called NodeList, which contains an ArrayList of strings (representing nodes) along with a distance. I am using Dijkstra's algorithm which involves opening up the shortest edge from each node. So, I use this loop to tabulate the possible routes from a given point:
for ( NodeList ed : edges )
{
if (ed.nodeList.get(0).equals(node1))
{
routes.add(ed);
}
}
This works fine, and appears to load the correct values into the variables (according to the debugger). However, when I try and set a variable to the distance of one of the nodes, I get a message "cannot find symbol variable distance". Here is the code that causes the problem:
int minDistance = routes.get(0).distance;
Is this abnormal or is there something obvious I'm missing?
如果节点列表不使用泛型,则编译器将无法确定您要get()
类型get()
这意味着它假设它是一个Object
,这意味着它没有distance
成员。
get(0)
apparently returned an object of class type which doesn't have the particular field.
There are several solutions depending on the root cause of the problem:
NodeList<Route>
instead of NodeList
) int distance = ((Route) routes.get(0)).distance)
public int distance;
) public int getDistance() { return this.distance; }
) and use it. Your code doesn't make sense. You say that you have an ArrayList
of Strings
, but you are looking for a member called 'distance'. String doesn't have a public field called 'distance'.
Most likely you need to declare ArrayList<SomeClassOfYours>
to fix this, but I can't tell what from what you posted.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.