Java LinkedList cannot find symbol size()

doing a graph problem on 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. 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]; 
    }
    
}

>Solution :

Your HashMap variable adjList returns an Object when you use .get(i) in:

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

As Object does not have a size method, this gives you an error. 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

Leave a Reply