简体   繁体   中英

Generic Singly Linked-List - Incompatible types

Here's the code:

import java.util.Scanner;

public class singlyyyyyy<E>{

   class Node<E>{
      private E element;
      private Node<E> next;

      public Node(E e, Node<E> n){
         element = e;
         next = n;
      }

      public E getElement(){
         return element;
      }

      public Node<E> getNext(){
         return next;
      }

      public void setNext(Node<E> n){
         next = n;
      }
   }

   private Node<E> head = null;
   private Node<E> tail = null;
   int size = 0;

   public int size(){
      return size;
   }

   public boolean isEmpty(){
      return size == 0;
   }

   public E first(){
      if(isEmpty())
         return null;

         return head.getElement();
   }

   public E last(){
      if(isEmpty())
         return null;

         return tail.getElement();
   }

   public void addFirst(E e){
      head = new Node<>(e, head);
      if(size == 0)
         tail = head;
         size++;
   }

   public void addLast(E e){
      Node<E> newNode = new Node<>(e, null);
      if(isEmpty())
         head = newNode;
      else
         tail.setNext(newNode);
         tail = newNode;
         size++;
   }

   public E removeFirst(){
      if(isEmpty())
         return null;
      E a = head.getElement();
      head = head.getNext();
      size--;

      if(size == 0)
         tail = null;
         return a;
   }

   @Override
    public String toString() {
    StringBuilder buf = new StringBuilder();
    buf.append('[');
    if (!isEmpty()) {
        buf.append(head.getElement());
        Node<E> nodeRef = head.getNext();
        while (nodeRef != null) {
            buf.append(", ");
            buf.append(nodeRef.getElement());
            nodeRef = nodeRef.getNext();
        }
    }
    buf.append(']');
    return buf.toString();
    }

     public void invoke(){
      Scanner scan = new Scanner(System.in);      
      singlyyyyyy<E> node = new singlyyyyyy<>();
      E e;
      System.out.print("Enter size : ");
      node.size();
      System.out.println("----------Menu----------");
      System.out.println("1. Add First Element    ");
      System.out.println("2. Add Last Element     ");
      System.out.println("3. Remove First         ");
      System.out.println("4. Display              ");
      System.out.println("0. Terminate Program    ");
      System.out.println("------------------------");
      int choice = scan.nextInt();

     switch(choice){
      case 1:
         E put;
         System.out.println("Add first element: ");
         put = scan.next();
         node.addFirst(put);
         break;
      case 2:
         E pin;
         System.out.println("Add last element: ");
         pin = scan.next();
         node.addFirst(pin);
         break;
      case 3:
         node.removeFirst();
         break;
      case 4:
         System.out.print(node);
         break;
      case 0:
         System.out.println("\nProgram Terminated.\n");
         break;
      }
   }
}

class Main{

   public static void main(){

      singlyyyyyy<Integer> singly = new singlyyyyyy<Integer>();

      singly.invoke();
   }
}

I understand I cannot use scan.next(); when prompting the user to input an element since E is generic and scan.next(); is used for Objects and Strings only. I've considered parsing put and pin to int but addFirst() and addLast() only accepts E .

You problem is that you want generic class but inside of it you know wha type you expect to be parameter. You have two option: Make your class non generic or provide a vay to work with it content without any knoledge about it.

First way is obviouse.

To implement second you can create interface which contains all method you need to work with elements. Like this:

interface Worker<E> {
    E convertFromString(String s);
    String convertToString(E e); 
}

Then pass implementation of thisinterface to your singlyyyyyy class and yse opropriate methods when you need to deal with E

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM