简体   繁体   English

实现泛型 Java LinkedList

[英]Implementing A Generic Type Java LinkedList

I'm pretty new to Java and I'm attempting to implement a Generic LinkedList Class in java.我对 Java 很陌生,我正在尝试在 Java 中实现一个通用的 LinkedList 类。 Below is the code yet it doesn't quite work right.下面是代码,但它并不完全正确。 I have some extra free time this semester and want to use this generic linkedlist to solve the linkedlist programming challenges in my interview test prep book.这学期我有一些额外的空闲时间,想使用这个通用链表来解决我的面试备考书中的链表编程挑战。 What am I doing wrong here?我在这里做错了什么? Why won't this work the way I want it to?为什么这不能按照我想要的方式工作?

Thanks for the help in advance.我在这里先向您的帮助表示感谢。

public class LinkedList {
    public static linkedlist ll;
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        ll = new linkedlist();
        Node one = new Node(1);
        Node two = new Node(2);
        Node three = new Node(3);
        Node four = new Node(4);
        System.out.println("s");
    }
    public static class linkedlist<T>{
        public Node head;
        public Node tail;
        int size;
        @SuppressWarnings("unchecked")
        public linkedlist(){
            size = 0;
        }
        void add(Class<T> typeParameterClass){
            if(head == null){
                head = new Node(typeParameterClass);
            }
            Node temp = new Node(typeParameterClass);
            Node headCopy = head;
            if(headCopy != null){
                while(headCopy.getNext()!= null){
                    headCopy = headCopy.getNext();
                }
                headCopy.setNext(temp);
            }
            size++;
        }
    } 
    public static class Node<T>{
        //final Class<T> typeParameterClass;
        Class<T> value;
        int intValue;
        Node next = null ;
        Node prev = null;
        public Node(Class<T> typeParameterClass){
            value = typeParameterClass; 
        }
        public Node(int i) {
            intValue = i;
            // TODO Auto-generated constructor stub
        }
        public Node getNext() {
            // TODO Auto-generated method stub
            return next;
        }
        public Node getPrev() {
            return prev;
        }
        public void setNext(Node temp){
            next = temp;
        }
    }
}

You would first spent some reading about Java naming conventions.您将首先阅读有关 Java 命名约定的一些内容。 Class names start Uppercase;类名以大写开头; always;总是; even for inner static classes.即使对于内部静态类。 You would also avoid using too many inner static classes in the first place.首先,您还应该避免使用过多的内部静态类。 In your example, there is absolutely no need to do it this way.在您的示例中,绝对没有必要这样做。 You would rather put the methods that make up a LinkedList directly on the LinkedList class.您宁愿将构成 LinkedList 的方法直接放在 LinkedList 类上。 You want that users of that class use that class;您希望该类的用户使用该类; and not some inner static thing like LinkedList.linkedlist or LinkedList.Node.而不是像 LinkedList.linkedlist 或 LinkedList.Node 这样的内部静态事物。

You see, right now, your methods are all on the inner Node class.你看,现在,你的方法都在内部Node类上。 So, do you want to deal with Nodes each time when doing something about your List?!那么,在对 List 执行某些操作时,您是否希望每次都处理节点?!

Then you read about how generics work in general.然后你会阅读泛型一般是如何工作的。 Example:例子:

Node one = new Node(1);

is probably not even compiling, but even when it does, it creates a raw type;甚至可能没有编译,但即使编译,它也会创建一个原始类型; as you do not have the type parameter there.因为那里没有类型参数。 You need something like:你需要这样的东西:

Node<Integer> one = new Node<>(1);

instead - you have to tell the compiler what real type you want to use instead of that anonymous T.相反 - 你必须告诉编译器你想使用什么真实类型而不是匿名 T。

In other words: start reading here .换句话说:从这里开始阅读。 Right now, you have like 25% knowledge/understanding;现在,您拥有大约 25% 的知识/理解; and that is not enough to start coding.这还不足以开始编码。

That is about what can be said without further description from your side about "what is not working" in your code.这就是关于在您的代码中没有进一步描述“什么不起作用”的情况下可以说的内容。 And even then: as said;即便如此:正如所说; your code is on such a low scale of "understanding" that the only reasonable answer is: step back and learn about the things you want to use.您的代码的“理解”程度如此之低,以至于唯一合理的答案是:退后一步,了解您想要使用的东西。

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

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