简体   繁体   English

无法将获取java.lang.Object转换为java.lang.Integer

[英]getting java.lang.Object cannot be converted to java.lang.Integer

I was implementing Graph data structure in Java. 我正在用Java实现Graph数据结构。 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. 基本上,我为该图创建一个LinkedLists数组,并为该图的顶点创建了每个链接列表。

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. 但是,我在行号24处无法将java.lang.Object转换为java.lang.Integer。对于为什么收到此错误一无所知。 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. 然后,它将知道lli的内容是整数。

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. LinkedList是原始类型,这意味着您丢失了有关列表中存储哪些对象的类型信息。 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. 我很惊讶你的代码编译,但我只想说的是说Integer i : lli你assumming该范围内的每一个对象lli是一个Integer ,当参数LinkedList不提供这样的保证。

To ensure that this will work, change LinkedList lli to LinkedList<Integer> lli . 为确保此方法有效, LinkedList lli更改为LinkedList<Integer> lli This means that every object in lli is an instance of Integer, thus the iteration won't fail. 这意味着lli中的每个对象都是Integer的实例,因此迭代不会失败。

When I try to run your code, my IDE warns me about the line 当我尝试运行您的代码时,IDE会警告我有关该行的信息

ll = new LinkedList[size]

Saying: 说:

Unchecked assignment: 'java.util.LinkedList[]' to 'java.util.LinkedList< java.lang.Integer >[]' 未经检查的赋值:“ java.util.LinkedList []”到“ 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. 对于您的情况,这主张使用ArrayList<LinkedList<Integer>>等。

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 . 看一下这个oracle教程 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. 名称gearRatio和currentGear是此约定的主要示例。

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

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