0

私は割り当てに取り組んでおり、現在、配列を上から下に移動しようとしているところに行き詰まっています。この配列は #1 ベースであるため、0 ではなく 1 から始まります。

イテレーターをテストするために、currentSize から配列を通過し、配列を下って、その中のすべてのオブジェクトを出力するリバース メソッドを付随させました。しかし、メソッドを実行していないようです。イテレータに何か問題があるか、リバースメソッドを書く方法があると思います。

最後に、テスターが反復子を呼び出したときに、配列を後方にトラバースできるようにする必要があります。

配列に追加する方法は次のとおりです。

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)
4

2 に答える 2

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++;
}
于 2015-02-22T01:13:58.990 に答える