I am doing lab, I viewed a lot of Java Generics example, but I cannot understand it. My goal is achieving Linked Stack. It have 2 files: IntStack.java and IntNode.java. The part of these code is:
public class IntNode { private int element = 0; private IntNode next = null; public IntNode(final int data, final IntNode next) { this.element = data; this.next = next; } public class IntStack { private IntNode top = null; public boolean isEmpty() { return this.top == null; } How to convert them to generics type? I know it should use <T>,and I write these code, it is correct or not?
public class Node<T> { private T element; private Node<T> next = null; public Node(final T data,final Node next) { this.element = data; this.next = next; } }
Node nextshouldn't use raw type, thus should be parametrized withT:Node<T> next.