![](/img/trans.png)
[英]Java - Trying to sort a LinkedList with a custom comparator, cannot find symbol sort
[英]Java LinkedList cannot find symbol size()
在 leetcode 上做一個圖形問題。 我對編碼很陌生,如果這很愚蠢,很抱歉。 我嘗試查看類似的問題,但沒有看到答案,非常抱歉。
目前正在做一個“克隆無向圖”的問題。
從調用的第一個函數創建鄰接列表后,我嘗試訪問一個 LinkedLists(它是 hashmap/adjList 的值)大小以及下一個函數中的值。 這不起作用並返回找不到符號錯誤。 我檢查了文檔,方法似乎正確。 我錯過了什么重要的東西嗎?
實際錯誤是:
Line 36: error: cannot find symbol
System.out.println(adjList.get(i).size());
^
symbol: method size()
location: class Object
非常感謝您的幫助。 對此,我真的非常感激。
class Solution {
public Node cloneGraph(Node node) {
if(node == null || node.neighbors.isEmpty()){
return null;
}
HashMap adjList = MakeAdjList(node);
System.out.println(adjList.get(1).size());
return reconstruct(adjList);
}
//undirectedGraph-> HashMap (adjList)
//uses BFS to go through each node in graph and creates adjacency list.
public HashMap MakeAdjList(Node node){
HashMap<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
HashSet<Integer> visited = new HashSet<Integer>();
LinkedList<Node> q = new LinkedList<Node>();
q.offer(node);
while(!q.isEmpty()){
Node currentNode = q.poll();
visited.add(currentNode.val);
map.put(currentNode.val, new LinkedList<Integer>());
for(Node neighbor : currentNode.neighbors){
if(!visited.contains(neighbor.val)){
q.offer(neighbor);
visited.add(neighbor.val);
}
map.get(currentNode.val).add(neighbor.val);
}
}
//traverses tree. if neighbor has already been met, or is already in queue, doesn't add to queue
// otherwise, adds neighbor to queue
//regardless, adds to value pair for this node's neighbors
return map;
}
//HashMap (adjList) -> ListOfNodes
// makes a node per adjList key. then gives each node pointers to and from the nodes listed in the adjList value list. returns a node within the graph
// returns node val 1
//!!!
public Node reconstruct(HashMap adjList){
Node[] nodes = new Node[adjList.size() + 1];
for(int i = 1; i<nodes.length+1; i++){
nodes[i] = new Node(i);
}
for(int j = 1; j< nodes.length; j++){
int l = adjList.get(j).size();
for(int i = 0; i< l; i++){
int neighborVal = adjList.get(j).get(i);
nodes[i].neighbors.add(nodes[neighborVal]);
}
}
return nodes[1];
}
}
在此語句中提供泛型類型的值:
HashMap adjList = MakeAdjList(node);
像這樣:
HashMap<Integer, List<Integer>> adjList = MakeAdjList(node);
當您在以下位置使用.get(i)
時,您的HashMap
變量adjList
返回一個Object
:
System.out.println(adjList.get(1).size());
由於Object
沒有size
方法,這會給你一個錯誤。 您需要轉換為您希望該值所屬的類。
或者您可以使用 Java 的泛型,如@CharchitKapoor所述
以下是您可以進行的更改,以便您的代碼編譯。
MakeAdjList()
的函數簽名更改為如下所示,這定義了Map<Integer, List<Integer>>
而不是HashMap
的返回類型: public Map<Integer, List<Integer>> MakeAdjList(Node node)
MakeAdjList()
時,請從此更改: HashMap adjList = MakeAdjList(node);
對此: Map<Integer, List<Integer>> adjList = MakeAdjList(node);
adjList.get(1).size())
這應該是編譯代碼所需的全部內容。
如果不進行這些更改,您對HashMap adjList = MakeAdjList(node)
的調用會返回一個基本的HashMap
,其中每個鍵都是java.lang.Object
類型,每個值也是java.lang.Object
。 即使您知道各種“鍵”實際上是Integer
,而“值”是java.util.List<Integer>
,編譯器也不知道這一點。 因此,如果您調用adjList.get(1)
它認為結果是一個Object
(不是List<Integer>
)並且它(正確地)通知您不能在Object
上調用size()
。
此外,在代碼的不同位置,您使用具體類作為左側類型,而不是使用接口。 例如:不要將左側定義為HashMap
,而是使用Map
。 這看起來像改變這個:
HashMap<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();
到下面的行(它使用Map
接口名稱,並在右側刪除重復的冗余類型信息,然后允許new HashMap<Integer, List<Integer>>()
成為簡單new HashMap<>()
):
Map<Integer, List<Integer>> map = new HashMap<>();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.