incompatible types : java.lang.Object cannot be converted to T

前端 未结 4 1966
心在旅途
心在旅途 2021-01-14 15:57

Here is my code:

package datastructures;

import java.util.Iterator;

public class Stack{
    private class Node{
        T data;
        N         


        
4条回答
  •  傲寒
    傲寒 (楼主)
    2021-01-14 16:57

    (For Java 8) You can use a typed variable with the diamond operator:

    private Node newNode(T data) {
        Node new_node = new Node<>();
        new_node.data = data;
        new_node.next = null;
        return new_node;
    }
    

    Otherwise the generic is initalized as Object and therefore the error is shown.

提交回复
热议问题