简体   繁体   English

Java LinkedList 找不到符号大小()

[英]Java LinkedList cannot find symbol size()

doing a graph problem on leetcode.在 leetcode 上做一个图形问题。 I'm pretty new to coding, so sorry if this is dumb.我对编码很陌生,如果这很愚蠢,很抱歉。 I tried looking at similar questions, but didn't see an answer, so sorry.我尝试查看类似的问题,但没有看到答案,非常抱歉。

Currently doing a 'clone an undirected graph' question.目前正在做一个“克隆无向图”的问题。

After the adjacency list is made from the first function called, I try to access one of the LinkedLists' (which is a value of the hashmap/adjList) size and also values in the next function.从调用的第一个函数创建邻接列表后,我尝试访问一个 LinkedLists(它是 hashmap/adjList 的值)大小以及下一个函数中的值。 This doesn't work and returns a cannot find symbol error.这不起作用并返回找不到符号错误。 I've checked the docs and the methods seem correct.我检查了文档,方法似乎正确。 Am i missing something important?我错过了什么重要的东西吗?

The actual error is:实际错误是:

Line 36: error: cannot find symbol
            System.out.println(adjList.get(i).size());
                                             ^
  symbol:   method size()
  location: class Object

Thanks a lot for the help.非常感谢您的帮助。 I really appreciate it.对此,我真的非常感激。

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]; 
    }
    
}

Provide the value of generic type in this statement:在此语句中提供泛型类型的值:

HashMap adjList = MakeAdjList(node);

like this:像这样:

HashMap<Integer, List<Integer>> adjList = MakeAdjList(node);

Your HashMap variable adjList returns an Object when you use .get(i) in:当您在以下位置使用.get(i)时,您的HashMap变量adjList返回一个Object

 System.out.println(adjList.get(1).size());

As Object does not have a size method, this gives you an error.由于Object没有size方法,这会给你一个错误。 You need to cast to the class you expect this value to belong to.您需要转换为您希望该值所属的类。

Or you can use Java's generics, as noted by @CharchitKapoor或者您可以使用 Java 的泛型,如@CharchitKapoor所述

Here are the changes you can make to so that your code compiles.以下是您可以进行的更改,以便您的代码编译。

  1. change function signature for MakeAdjList() to be like below, this defines a return type of Map<Integer, List<Integer>> instead of HashMap :MakeAdjList()的函数签名更改为如下所示,这定义了Map<Integer, List<Integer>>而不是HashMap的返回类型:
     public Map<Integer, List<Integer>> MakeAdjList(Node node)
  2. when you call MakeAdjList() , change from this:当您调用MakeAdjList()时,请从此更改:
     HashMap adjList = MakeAdjList(node);
    to this:对此:
     Map<Integer, List<Integer>> adjList = MakeAdjList(node);
  3. once you've done #2, you can call adjList.get(1).size()) without issues一旦你完成了#2,你就可以毫无问题地调用adjList.get(1).size())

That should be all you need to get your code compiling.这应该是编译代码所需的全部内容。

Without making those changes, your call to HashMap adjList = MakeAdjList(node) comes back with a basic HashMap where each key is type java.lang.Object and each value is also java.lang.Object .如果不进行这些更改,您对HashMap adjList = MakeAdjList(node)的调用会返回一个基本的HashMap ,其中每个键都是java.lang.Object类型,每个值也是java.lang.Object Even though you know that the various "keys" are in fact Integer , and the "values" are java.util.List<Integer> , the compiler doesn't know that.即使知道各种“键”实际上是Integer ,而“值”是java.util.List<Integer>编译器也不知道这一点。 So if you call adjList.get(1) it thinks the result is an Object (not List<Integer> ) and it (correctly) informs you that you cannot call size() on an Object .因此,如果您调用adjList.get(1)它认为结果是一个Object (不是List<Integer> )并且它(正确地)通知您不能在Object上调用size()

Additionally, in various places in your code you're using a concrete class as the left-hand type, instead of using the interface.此外,在代码的不同位置,您使用具体类作为左侧类型,而不是使用接口。 For example: instead of defining the left-hand side as HashMap , use Map instead.例如:不要将左侧定义为HashMap ,而是使用Map That looks like changing this:这看起来像改变这个:

HashMap<Integer, List<Integer>> map = new HashMap<Integer, List<Integer>>();

to the line below (which uses the Map interface name, and also drops the repeated, redundant type information on the right-hand side, which then allows new HashMap<Integer, List<Integer>>() to become simply new HashMap<>() ):到下面的行(它使用Map接口名称,并在右侧删除重复的冗余类型信息,然后允许new HashMap<Integer, List<Integer>>()成为简单new HashMap<>() ):

Map<Integer, List<Integer>> map = new HashMap<>();

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

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