2013-03-23 89 views
1


我有一個堆棧的ArrayList,其中我添加一個元素到一個堆棧,並遍歷列表打印每個堆棧的索引。

然後,我從前一個堆棧中刪除該元素,將其添加到下一個堆棧,打印每個堆棧的索引,然後繼續處理ArrayList中的所有堆棧。

但是,當任何堆棧爲空時,在獲取ArrayList中每個堆棧的索引時會出現非常不尋常的行爲。這是空堆棧將有正確的索引值,而棧是空將有不正確的索引值。

此外,如果包含一個或多個元素的堆棧位於索引0處,則所有其他索引值將爲1.如果包含元素的堆棧位於任何其他索引處,則它將具有正確的索引值和所有其他指數值將是0

在堆棧的ArrayList中,如果堆棧爲空,爲什麼索引不正確?



這裏是我的代碼:

import java.util.List; 
import java.util.Stack; 
import java.util.ArrayList; 

public class ListOfStacks { 

    // instance variables: 
    List<Stack<Integer>> stacks; 
    private static final int NUMBER_OF_STACKS = 3; 

    // constructor: 
    ListOfStacks() { 
     this.stacks = new ArrayList<Stack<Integer>>(NUMBER_OF_STACKS); 

     // adding the stacks to the list here: 
     for (int i = 0; i < NUMBER_OF_STACKS; i++) { 
      this.stacks.add(new Stack<Integer>()); 
     } 
    } 

    // instance methods: 
    void addElement(int stackIndex, int element) { 
     this.stacks.get(stackIndex).add(element); 
    } 
    void removeElement(int stackIndex) { 
     this.stacks.get(stackIndex).pop(); 
    } 
    void printIndexes(int stackIndex, int element) { 
     System.out.printf("The stack at index %d now contains %d" + 
      "(the other stacks are empty):%n", stackIndex, element); 

     for (Stack<Integer> stack : this.stacks) { 
      System.out.printf("index %d%n", this.stacks.indexOf(stack)); 
     } 
     System.out.println(); 
    } 

    // main method: 
    public static void main(String[] args) { 
     ListOfStacks list = new ListOfStacks(); 
     int index = 0, number = 5; 

     // adding the number 5 to the stack at index 0: 
     list.addElement(index, number); 
     list.printIndexes(index, number); 

     // now removing that element, and adding it to the stack at index 1: 
     list.removeElement(index++); 
     list.addElement(index, number); 
     list.printIndexes(index, number); 

     // now removing that element, and adding it to the stack at index 2: 
     list.removeElement(index++); 
     list.addElement(index, number); 
     list.printIndexes(index, number); 
    } 
} // end of ListOfStacks 


...這裏是輸出(三棧的ArrayList):

The stack at index 0 now contains 5 (the other stacks are empty): 
index 0 
index 1 
index 1 

The stack at index 1 now contains 5 (the other stacks are empty): 
index 0 
index 1 
index 0 

The stack at index 2 now contains 5 (the other stacks are empty): 
index 0 
index 0 
index 2 


回答

6

的原因,你得到了錯誤的索引號與indexOf在列表中實現的方式做。在它下面撥打Stack.equals()。這決定了堆棧是否等於元素明智。當您用空棧調用list.indexOf時,它將返回列表中第一個空棧的索引。

+0

感謝@DeltaLima的幫助,那麼如何才能實現具有空棧的「正常」索引行爲? – 2013-03-23 20:41:31

+0

那麼......沒有關於你想要達到的東西的更多知識,這有點難以回答。我想知道列表的選擇是否正確。這裏不是一個簡單的數組嗎? – DeltaLima 2013-03-23 20:48:38

+0

感謝@DeltaLima,我曾嘗試使用堆棧的數組之前,但有幾個錯誤(請參閱:http://stackoverflow.com/questions/15530118/how-can-i-instantiate-an-array-of-stacks- of-type/15530140#comment22001592_15530140)。我的目的是創建一個合適的結構來解決河內塔問題。 – 2013-03-23 20:56:38

1
indexOf(Object o) 
      Returns the index of the first occurrence of the specified element in this list, or -1 if this list does not contain the element. 

在Java堆棧基本上是一個向量,向量類有此爲等於

/** 
    969  * Compares the specified Object with this Vector for equality. Returns 
    970  * true if and only if the specified Object is also a List, both Lists 
    971  * have the same size, and all corresponding pairs of elements in the two 
    972  * Lists are <em>equal</em>. (Two elements {@code e1} and 
    973  * {@code e2} are <em>equal</em> if {@code (e1==null ? e2==null : 
    974  * e1.equals(e2))}.) In other words, two Lists are defined to be 
    975  * equal if they contain the same elements in the same order. 
    976  * 
    977  * @param o the Object to be compared for equality with this Vector 
    978  * @return true if the specified Object is equal to this Vector 
    979  */ 
    980  public synchronized boolean equals(Object o) { 
    981   return super.equals(o); 
    982  } 

所以發生的是的indexOf是找到的第一個空棧把它看作平等並返回一個指數。

因此,當索引0具有元件和其他人沒有元素第一堆棧等於空堆棧的位置是1

如果另一個元件具有數據和第一元素等於和你正在尋找一個空的堆棧將始終停止並返回索引0.

+0

感謝@Midpipps,所以我需要重寫'.equals(Object o)'和/或'.indexOf(Object o)'來實現「正常」的索引行爲嗎? – 2013-03-23 20:39:26

+1

您可以創建一個擴展堆棧類並覆蓋equals方法成爲引用等式的類。我不知道這是否是最好的方法,但它應該給你你正在尋找的答案。 – Midpipps 2013-03-23 21:12:16