简体   繁体   中英

getting java.lang.Object cannot be converted to java.lang.Integer

I was implementing Graph data structure in Java. Here is my implementation:

    package Graph;

    import java.util.*;
    import java.io.*;

   public class Graphs
   {
       int size;
       LinkedList<Integer>[] ll;

       Graphs(int size)
       {
            this.size = size;

            ll = new LinkedList[size];

            for(int i=0; i<size; i++)
              ll[i] = new LinkedList<Integer>();

       }

       public static void print(LinkedList lli)
       {
            for(Integer i: lli)
                System.out.println(i);

            //for(int i=0; i<lli.size(); i++)
            //    System.out.println(lli.get(i));
       }

       public static void addEdge(Graphs graph, int up, int to)
       {
           graph.ll[to].add(up);
       }

      public static void main(String args[])
      {
          int V=5;
          Graphs graph = new Graphs(V);

          addEdge(graph,1,2);
          addEdge(graph,1,3);
          addEdge(graph,2,3);
          addEdge(graph,3,1);
          addEdge(graph,3,2);
          addEdge(graph,3,4);
          addEdge(graph,4,3);

          print(graph.ll[3]);      
      }

   }

Basically I am creating an array of LinkedLists for the graph with each linked list for a vertex of the graph.

However, I am getting a java.lang.Object cannot be converted to java.lang.Integer at line number 24. Clueless as to why am I getting this error. Any suggestions as to what am I missing?

Declare the print method like this:

public static void print(LinkedList<Integer> lli)

Then it will know that the contents of lli are integers.

The specific issue you're having is with your print function:

public static void print(LinkedList lli){
        for(Integer i: lli)
            System.out.println(i);
}

LinkedList is a raw type, meaning you lose type information about what kinds of objects are stored in the list. As a general rule, raw types are a bad idea . I'm very surprised your code compiled, but suffice to say that by saying Integer i : lli you're assumming that every object within lli is an Integer when the parameter LinkedList provides no such guarantee.

To ensure that this will work, change LinkedList lli to LinkedList<Integer> lli . This means that every object in lli is an instance of Integer, thus the iteration won't fail.

When I try to run your code, my IDE warns me about the line

ll = new LinkedList[size]

Saying:

Unchecked assignment: 'java.util.LinkedList[]' to 'java.util.LinkedList< java.lang.Integer >[]'

Which indicates that something fishy is going on here.

Mixing Lists and Arrays gets a bit messy with generic typing - it's a lot easier and cleaner to just do lists of lists if you need size mutability, or a multi-dimension array if not. For your case, that argues for a ArrayList<LinkedList<Integer>> or the like.

We can fix the issues by resolving the generic issues:

import java.util.*;
import java.io.*;

public class Graphs
{
  int size;
  ArrayList<LinkedList<Integer>> ll;

  Graphs(int size)
  {
    this.size = size;

    ll = new ArrayList<LinkedList<Integer>>();

    for(int i=0; i<size; i++)
      ll.add(new LinkedList<Integer>());

  }

  public static void print(LinkedList<Integer> lli)
  {
    for(Integer i: lli)
      System.out.println(i);

    //for(int i=0; i<lli.size(); i++)
    //    System.out.println(lli.get(i));
  }

  public static void addEdge(Graphs graph, int up, int to)
  {
    graph.ll.get(to).add(up);
  }

  public static void main(String args[])
  {
    int V=5;
    Graphs graph = new Graphs(V);

    addEdge(graph,1,2);
    addEdge(graph,1,3);
    addEdge(graph,2,3);
    addEdge(graph,3,1);
    addEdge(graph,3,2);
    addEdge(graph,3,4);
    addEdge(graph,4,3);

    print(graph.ll.get(3));
  }
}

I copied your code and it didn't compile until I changed

public static void print(LinkedList lli)

to:

public static void print(LinkedList<Integer> lli)

From where it worked without problems

Also giving variables a name which starts with an upper case letter is against the naming convention. Have a look at this oracle tutorial . The last bullet point on the page states:

If the name you choose consists of only one word, spell that word in all lowercase letters. If it consists of more than one word, capitalize the first letter of each subsequent word. The names gearRatio and currentGear are prime examples of this convention.

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.

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