我的LinkedList的set方法有什么问题

时间:2014-06-04 06:52:41

标签: java collections arraylist implementation

我试图实现一个链表。除了set方法之外,我的所有方法都有效。它只应该更改给定索引处的元素替换。但是在索引中设置元素之后,它会使列表中的其余元素为null,有人能指出我在set方法中做错了吗?

        public class LArrayList<E> {

            private static class Node<E> {

                Node<E> next;

                E data;

                public Node(E dataValue) {
                    next = null;
                    data = dataValue;
                }

                @SuppressWarnings("unused")
                public Node(E dataValue, Node<E> nextValue) {
                    next = nextValue;
                    data = dataValue;
                }

                public E getData() {
                    return data;
                }

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

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

            private Node<E> head;
            private static int listCount;

            public LArrayList() {

                head = new Node<>(null);
                listCount = 0;
            }

            public void add(int index, E e) throws IndexOutOfBoundsException{

                if (index < 0 || index >= listCount + 1) {

                    throw new IndexOutOfBoundsException("Bad index, please use index within range");
                }
                else{


                Node<E> positionTemp = new Node<>(e);
                Node<E> positionCurrent = head;

                for (int i = 0; i < index && positionCurrent.getNext() != null; i++) {
                    positionCurrent = positionCurrent.getNext();
                }

                positionTemp.setNext(positionCurrent.getNext());
                positionCurrent.setNext(positionTemp);
                listCount++;
            }
            }

            public E set(int index, E e) throws IndexOutOfBoundsException {

                if (index < 0 || index >= listCount) 
                    throw new IndexOutOfBoundsException("Bad index, please use index within range");


                Node<E> positionTemp = new Node<>(e);
                Node<E> positionCurrent = head;

                for (int i = 0; i < index; i++) {
                    positionCurrent = positionCurrent.getNext();
                }


                positionCurrent.setNext(positionTemp);

                return positionCurrent.getData();

            }

    public E get(int index) throws IndexOutOfBoundsException
    {

        if (index < 0 || index >= listCount)
            throw new IndexOutOfBoundsException("Bad index, please use index within range");

        Node<E> positionCurrent = head.getNext();
        for (int i = 0; i < index; i++) {
            if (positionCurrent.getNext() == null)
                return null;

            positionCurrent = positionCurrent.getNext();
        }
        return positionCurrent.getData();
    }



            public static void main(String[] args) {

                LArrayList<String> aa = new LArrayList<String>();
                aa.add(0, "0");
                aa.add(1, "1");
                aa.add(2, "2");
                aa.add(3, "3");
                aa.add(4, "4");
                aa.add(5, "5");


                System.out.println("The contents of AA are: " + aa);    

                 aa.set(0, "a");


                System.out.println("The contents of AA are: " + aa);    



                 System.out.println(aa.get(0));

                 System.out.println(aa.get(1));

                 System.out.println(aa.get(2));
                 System.out.println(aa.get(3));

                 System.out.println(aa.get(4));

                 System.out.println(aa.get(5));

        //OUTPUT IS: The contents of aa are: [0][1][2][3][4][5]
        //The contents of AA are: [a][b][c][d][e]
        //a
        //A
        //null
        //null
        //null
        //null

            }

        }

3 个答案:

答案 0 :(得分:1)

快速查看代码后:

 positionCurrent.setNext(positionTemp);

你还需要将 positionTemp 链接到之后的元素吗?

答案 1 :(得分:1)

首先获取当前索引之前的项目:

           for (int i = 0; i < index; i++) {
                positionCurrent = positionCurrent.getNext();
            }

您缺少当前设置项目与列表其余部分之间的链接,请按以下方式建立:

 positionTemp.setNext(positionCurrent.getNext().getNext());
 positionCurrent.setNext(positionTemp);

并返回

 positionCurrent.getNext().getData()

希望它有所帮助。

答案 2 :(得分:0)

好的,所以你的代码中有很多问题,我会帮助你一些但不是全部。 我真的建议你使用Eclipse调试器(如果你使用eclipse),请检查一下: http://www.vogella.com/tutorials/EclipseDebugging/article.html

首先,一个集合后你的整个列表为空的原因是你的索引是0而不是你使用什么都不做的for循环(因为索引是0),所以在那之后的行:

                positionCurrent.setNext(positionTemp);

你正在设置head的下一个是positionTemp,而你正在丢失列表的其余部分,因为你没有为postionTemp设置下一个(postionTemp next为null)

第二,当您将第一个项目添加到列表中,或者当列表为空并且您要添加内容时,您需要将此新节点作为列表的头部,否则您的列表头将始终为空。

第三,当你想打印清单时,你不能使用:

                System.out.println("The contents of AA are: " + aa);    

你需要这样做:

    Node<E> head = aa.head;
    while(head != null){
        system.out.println(head.data)
    }

您的代码看起来有更多错误,所以我建议您使用调试器。 祝你好运

相关问题