简体   繁体   中英

How implement Java Generics

I think I set up my class correct to be generic but when i try to call methods i cant seem to set up my other methods correct. Im I supposed to cast my variables to be generic? or do I cast my methods to variables?

public class LinkedList<E>
{
    // reference to the head node.
    private E head;
    private int listCount;


    public boolean delete(E string)
    // post: removes the element at the specified position in this list.
    {       
        Node current = head.getNext();

        while(true){
            if(current == null){
                return false;
            }else if(current.getNext().getData().equals(string)){
                if(current.getNext() == null){
                    current.setNext(null);
                }else{
                    current.setNext(current.getNext().getNext());
                }
                listCount--; // decrement the number of elements variable
                return true;
            }else{
                current = current.getNext();
            }   
        }
    }

  private class Node<E extends Comparable<E>>
    {
        // reference to the next node in the chain,
        E next;
        // data carried by this node.
        // could be of any type you need.
        E data;


        // Node constructor
        public Node(E _data)
        {
            next = null;
            data = _data;
        }

        // another Node constructor if we want to
        // specify the node to point to.
        public Node(E _data, E _next)
        {
            next = _next;
            data = _data;
        }

        // these methods should be self-explanatory
        public E getData()
        {
            return data;
        }

        public void setData(E _data)
        {
            data = _data;
        }

        public E getNext()
        {
            return next;
        }

        public void setNext(E _next)
        {
            next = _next;
        }
    }


}

The types of your variables were a bit messed up.

  • Node.next needs to be a Node
  • LinkedList.head needs to be Node
  • Node does not need to be generic. (The E type parameter is in scope for the inner class.)

Here's a version that compiles:

class LinkedList<E> {
    // reference to the head node.
    private Node head;
    private int listCount;

    public boolean delete(E string)
    // post: removes the element at the specified position in this list.
    {
        Node current = head;

        while (true) {
            if (current == null) {
                return false;
            } else if (current.getData().equals(string)) {
                if (current.getNext() == null) {
                    current.setNext(null);
                } else {
                    current.setNext(current.getNext().getNext());
                }
                listCount--; // decrement the number of elements variable
                return true;
            } else {
                current = current.getNext();
            }
        }
    }

    private class Node {
        // reference to the next node in the chain,
        Node next;
        // data carried by this node.
        // could be of any type you need.
        E data;

        // Node constructor
        public Node(E _data) {
            next = null;
            data = _data;
        }

        // another Node constructor if we want to
        // specify the node to point to.
        public Node(E _data, Node _next) {
            next = _next;
            data = _data;
        }

        // these methods should be self-explanatory
        public E getData() {
            return data;
        }

        public void setData(E _data) {
            data = _data;
        }

        public Node getNext() {
            return next;
        }

        public void setNext(Node _next) {
            next = _next;
        }
    }

}

Looking at your delete method, I think it's a bit buggy though. When you arrive at a node where the data equals string , you change the next-pointer of that node while you should be changing the next-pointer of the previous node.

I would try something like this:

    Node current = head, prev = null;
    while (current != null) {
        if (current.getData().equals(string)) {
            // Remove current from list
            if (current == head) {
                head = current.getNext();
            } else {
                prev.setNext(current.getNext());
            }

            listCount--; // decrement the number of elements variable
            return true;
        }
        prev = current;
        current = current.getNext();
    }

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