简体   繁体   English

通用单链表-不兼容的类型

[英]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(); 我知道我不能使用scan.next(); when prompting the user to input an element since E is generic and scan.next(); 由于E为泛型且scan.next();时提示用户输入元素时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 . 我已经考虑过putpin解析为intaddFirst()addLast()仅接受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 然后在需要处理Esinglyyyyyy实现传递给singlyyyyyy类和yse适当的方法

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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