为什么我的reverse()方法反转MyLinkedList对象?

时间:2014-06-24 01:04:27

标签: java this reverse doubly-linked-list

我正在制作一个Doubly-Linked-List类,但我坚持使用这个reverse()方法。我希望reverse()方法生成并返回一个新旧的MyLinkedList,而不更改旧的MyLinkedList。

public MyLinkedList<AnyType> reverse()
{
   MyLinkedList<AnyType> list = this;

   for(int i=0,j=list.theSize-1; i < list.theSize/2; i++, j--)
       list.swap(i,j);

   return list;
}

public void swap(int index1, int index2)
{
        if(index1 == index2)
            return;
        else if(index1 > theSize-1 || index2 > theSize-1)
        {
            try{
                throw new IndexOutOfBoundsException();
            }
            catch(IndexOutOfBoundsException e)
            {
                System.out.println("IndexOutOfBoundsException\n");
                return;
            }             
        }
        Node<AnyType> first;
        Node<AnyType> last;
        if(index1 < index2)
        {
            first = this.getNode(index1);
            last = this.getNode(index2);
        }
        else
        {
            first = this.getNode(index2);
            last = this.getNode(index1);
        }
        if((index1 == 0 && index2 == theSize -1) ||(index1 == theSize -1 && index2 == 0))
        {   
            first.next.prev = last;
            last.prev.next = first;

            first.prev = last.prev;
            last.next = first.next;

            first.next = endMarker;
            endMarker.prev = first;
            last.prev = beginMarker;
            beginMarker.next = last;
        }

        else if(Math.abs(index1-index2) == 1)
        {
            first.next = last.next;
            last.prev = first.prev;
            first.prev = last;
            last.next = first;
            last.prev.next = last;
            first.next.prev = first;
        }

        else
        {
            Node<AnyType> tempNext = last.next;
            Node<AnyType> tempPrev = last.prev;

            last.next.prev = first;
            last.prev.next = first;
            first.prev.next = last;
            first.next.prev = last;

            last.next = first.next;
            last.prev = first.prev;
            first.next = tempNext;
            first.prev = tempPrev;
        }
}
public static void main( String [ ] args )
{
    MyLinkedList<Integer> lst = new MyLinkedList<>( );
    MyLinkedList<Integer> lst2 = new MyLinkedList<>( );

    for( int i = 0; i < 10; i++ )
            lst.add( i );
    System.out.println("Original List: " + lst);

    lst2 = lst.reverse();
    System.out.println("Original List: " + lst);
    System.out.println("Reversed List: " + lst2);
}

这是我的输出:

Original List: [ 0 1 2 3 4 5 6 7 8 9 ]
Original List: [ 9 8 7 6 5 4 3 2 1 0 ]
Reversed List: [ 9 8 7 6 5 4 3 2 1 0 ]

正如您所看到的,原始列表也正在逆转。为什么会这样?

2 个答案:

答案 0 :(得分:2)

这就是原因:

lst2 = lst.reverse();

您在reverse()上呼叫lst,因此lst将被撤销。然后分配将要保留的结果ot lst2。解决方案是制作lst的深层副本,分配lst2并致电

lst2.reverse();

而不是:MyLinkedList<AnyType> list = this;

试试这个:

for( int i = 0; i < 10; i++ )
    list.add( this.getNode(i) );

答案 1 :(得分:2)

  

为什么我的reverse()方法反转MyLinkedList对象?

因为你没有拥有两个对象。您只有一个具有两个引用的对象。因此,您反转对象中的顺序,因此通过两个引用都可以看到新订单。

相关问题