ClassCastException : cant be cast to java.lang.Integer

前端 未结 4 633
谎友^
谎友^ 2021-01-07 14:20

I know this has been asked a lot in stackoverflow but I could not find answers that work to my problem.

In the following code below, I cant print out each item in

相关标签:
4条回答
  • 2021-01-07 14:49
    private HashMap<Integer, ArrayList<Integer>>  edges;
    
    // ...later
    
    Edge e = new Edge(from,to,T);
    
    // ...later
    
    else {
        tmp = new ArrayList();
        edges.put(e.from,tmp);
    }
    tmp.add(e);
    

    Ultimately, this is a classic example of why raw types are bad. You've got an ArrayList<Integer> and you put Edges in it.

    Unfortunately, I don't know how to tell you how to fix it since I don't understand what you're trying to do.

    0 讨论(0)
  • 2021-01-07 14:49

    You declare:

    int from;
    int to;
    int type;
    

    and you should declare them as Integers instead:

    Integer from;
    Integer to;
    Integer type;
    

    because later on you're doing:

    this.from = new Integer(from);
    

    etc.

    A better option would be to change the assignment to:

    this.from = from;
    

    which would also solve this error since you would be assigning an int to an int. Is there a purpose you're using new Integer() ? because if not - I would suggest removing it - it's slower (performance-wise) comparing to the primitive int.

    Bottom line: stay consistent and either use int throughout the code, or Integer - try not to mix them unless it's really required.

    0 讨论(0)
  • 2021-01-07 14:51

    the error lies here

      public void addEdgeForIndexing(int from, int to, int T) throws IllegalArgumentException, IllegalAccessException {   
    
                Edge e = new Edge(from,to,T);
                nodeIDs.add(e.from);
                nodeIDs.add(e.to);
    
                ArrayList tmp = null;
                if (edges.containsKey(e.from))
                  tmp = (ArrayList) edges.get(e.from);
                else {
                  tmp = new ArrayList();
                  edges.put(e.from,tmp);
                }
                tmp.add(e);//adding an edge to tmp
    

    later in the code you get the ArrayList out of the Map as ArrayList but it an ArrayList containing Edge try to change

    tmp = new ArrayList();
    

    to

    tmp = new ArrayList<Integer>();
    

    you should get a compilation error when adding an Edge to it

    0 讨论(0)
  • 2021-01-07 14:59
        public ArrayList<Integer> getOutEdgesToP(int id) {
    if (!edges.containsKey(id)) {
        return null;
    }
    System.out.println(edges.get(id));
    ArrayList<Integer> a = edges.get(id);
    System.out.println("Arraylist a: " + a); // if i print using this its
                         // okay. but i cant get each
                         // item in this ArrayList like
                         // below
    for (Object item : a) { // the error of classcastexception is here
        System.out.println(item);
    }
    
    return a;
    }
    

    This should work. I don't have a lot of time to search for an explanation.

    0 讨论(0)
提交回复
热议问题