如何在无序数组中向后遍历?

时间:2015-02-22 00:47:52

标签: java arrays iterator computer-science unordered

我正在完成一项任务,目前我仍然试图从上到下完成我的阵列。这个数组基于#1,因此从1开始而不是0开始。

为了测试我的迭代器,我做了一个附带的反向方法,从currentSize和gos向下遍历数组,打印出其中的每个对象。但似乎没有执行该方法。我假设我的迭代器可能有问题,或者我编写反向方法。

Ultimatley,当测试人员调用迭代器时,我需要让数组能够向后反转。

以下是将内容添加到数组中的方法:

public void addFirst(E obj) {
    if(currentSize==maxSize)
        growStrorage();

    if(isEmpty())
        storage[1]=obj;

    for(int i=1; i>currentSize+1; i++){
        storage[i+1]=storage[i];
    }

    storage[1]=obj;
    modCounter++;
    currentSize++;
}

这是相反的方法(这是作为测试方法来帮助解决我的问题):

public void reverseList() {

        for(int i=currentSize; i>=1;i--)
            System.out.println(storage[i]);


    }

这是我要求的showMe方法(这也是作为帮助解决我的问题的测试方法):

public void showMe(){

    for(int i=1; i<=currentSize; i++)
        System.out.print(i+" ");


}

这是我的迭代器:

    public Iterator<E> iterator() {
    return new IteratorHelper();
}

private class IteratorHelper <E>implements Iterator <E>{

    private int iterIndex;
    long stateCheck;

    public IteratorHelper(){
        iterIndex=1;
        stateCheck=modCounter;
    }

    public boolean hasNext(){
        if(stateCheck !=modCounter){
            throw new ConcurrentModificationException();
        }

        return iterIndex<=currentSize;
    }

    public E next(){
        if(!hasNext())
            throw new NoSuchElementException();

            return (E)storage[iterIndex++];

    }

这是测试人员:

 for(int i=1; i <= 10; i++)
        list.addFirst(new Integer(i));  
    System.out.println("Current size of list: (should be 10)" 
        + list.size()); 

 //My code 
   System.out.println("Now showing what is in Array..");
       list.showMe();
       System.out.println("\n");  
   System.out.println("Now reversing Array..");
    list.reverseList();


  *******  System.out.println("Now using the iterator, should print " *******
        + "10 .. 1");            
    for(int x : list)
        System.out.print(x + " ");
    System.out.println(); 

****** ==我遇到问题的地方

这是印刷品:

 Should print 1 .. 10
  1 2 3 4 5 6 7 8 9 10 

   Now removing them all
   Current size of list: (should be zero) 0
    Current size of list: (should be 10)10
   Now showing what is in Array..
   1 2 3 4 5 6 7 8 9 10 

   Now reversing Array..
   null
   null
   null
   null
   null
   null
   null
   null
   null
   10
  Now using the iterator, should print 10 .. 1
  10 ERROR java.lang.NullPointerException
  java.lang.NullPointerException
    at data_structures.P1Tester.runTests(P1Tester.java:49)
    at data_structures.P1Tester.<init>(P1Tester.java:14)
    at data_structures.P1Tester.main(P1Tester.java:103)

2 个答案:

答案 0 :(得分:0)

在您的代码中,当反向移动时,似乎您的for循环不正确。我假设它应该是i >= 0而不是i<=1,这是永远不会的。此外,你应该打印出超过i的东西,这只是索引。

public void reverseList() {

    for(int i=currentSize; i >= 0;i--)
        System.out.println(i);

}

代码中您已交换<>

的另一个位置

此外,此代码将设置存储[2] =存储[1],存储[3] =存储[2]等,这意味着所有i的存储[i] =存储[1]。基本的bug。它可能应该是这样的:

for(int i = currentSize; i >= 1; i++){
    storage[i+1]=storage[i];
}

答案 1 :(得分:0)

问题是如何处理addFirst方法中的现有内容以及使用growStorage。

public void addFirst(E obj) {
    if(currentSize==maxSize)
        growStrorage();

    if(isEmpty())
        storage[1]=obj;

    for(int i=1; i>currentSize+1; i++){
        storage[i+1]=storage[i];
    }

    storage[1]=obj;
    modCounter++;
    currentSize++;
}

复制存储时,必须保留对当前存储的引用以复制当前内容。您还应该使用System.arrayCopy而不是手动复制内容。类似的东西:

/**
 * Grows the backing storage and optionally shifts all the content
 * when copying to new array.
 */
private void growStorage(int shift) {
    final Object[] current = storage;
    //double current size
    storage = new Object[current.length * 2];
    maxSize = storage.length;

    System.arrayCopy(current, 0, storage, shift, current.length);
}

public void addFirst(E obj) {
    if(currentSize==maxSize)
        growStrorage(1);

    storage[1]=obj;

    modCounter++;
    currentSize++;
}
相关问题