0

I am writing a basic code for Singly linked list and found that there is a compiler error and have to use @SuppressWarnings("unchecked") to get rid of it.

I am wondering what's causing this unchecked error? Thank you.

public class SinglyLinkedList1<T> {

  private class Node<T>{
    private T data;
    private Node next;
    public Node (T item, Node<T> next) {
      this.data = item;
      this.next = next;
    }
    public Node (T item) {
      this.data = item;
      this.next = null;
    }
  }

  private Node<T> head;

  public SinglyLinkedList1() {
    this.head = null;
  }

  public void add(T item) {
    if (head == null) {
      head = new Node<T>(item);
    } else {
      head.next= new Node<T>(item);
    }
  }


  @SuppressWarnings("unchecked")
  public void print() {
    if (head == null) {
      System.out.println("null");
    } else {
      Node<T> current = head;
      while (current != null) {
        System.out.println("data: " + current.data);
        current = current.next;
      }
    }
  } 


  public static void main(String[] args) {
    SinglyLinkedList1<Integer> List1= new SinglyLinkedList1<Integer> ();
    List1.add(1);
    List1.print();
  }

}
ZackJ
  • 1
  • 1

0 Answers0