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

前端 未结 4 1969
心在旅途
心在旅途 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:47

    Node's data field doesn't know it's type.

    Try giving the type when you initialize head.

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

    Now, new_node knows that the data field is of type T.

提交回复
热议问题