2017-02-04 7 views
1

私はJavaの初心者ですから、Iterator<Iterator<Integer>>などの値を受け取る必要があります。例えば、我々が持っている可能性がありますイテレータを使用して2次元配列を1D配列のように繰り返します。

{{1, 2}, {3, 4}, {5, 6}} 

next()の結果は1でなければなりません。 next()もう一度試してみると、234などです.1D配列から値を1つずつ取得するのと同様ですが、2次元配列から取得すると同じです。私たちはをコピーしないでください何かをコピーしてください。だから、私はいくつかの悪いコードの下に書いた:

public class IteratorNext { 

    private Iterator<Iterator<Integer>> values = null; 
    private Iterator<Integer> current; 

    public IteratorNext(Iterator<Iterator<Integer>> iterator) { 
     this.values = iterator; 
    } 

    public int next() throws NoSuchElementException { 
     current = values.next(); 
     if (!current.hasNext()) { 
      values.next(); 
     } 
     if (!values.hasNext() && !current.hasNext()) { 
      throw new NoSuchElementException("Reached end"); 
     } 
     return current.next(); 
    } 
} 

このコードはnext()の結果は、その後、その後、531あるので、正しくないとので、ここでは例外の。これを修正するには?

+0

あなたは 'のjava-8'を使用していますか?これを行う簡単な方法があります。 – CKing

答えて

1

ような何かを行う必要があります(array2dを仮定することができますあなたの2次元配列への参照)であること:

Arrays.stream(array2d).flatMapToInt(Arrays::stream).forEach(System.out::println); 

あなたのソリューションに固執したい場合は、次のようにあなたのnext方法を変更する必要があります:

public int next() throws NoSuchElementException { 
    int result = -1; 
    //Are we already iterating one of the second dimensions? 
    if(current!=null && current.hasNext()) { 
     //get the next element from the second dimension. 
     result = current.next(); 
    } else if(values != null && values.hasNext()) { 
     //get the next second dimension 
     current = values.next(); 
     if (current.hasNext()) { 
      //get the next element from the second dimension 
      result = current.next(); 
     } 
    } else { 
     //we have iterated all the second dimensions 
     throw new NoSuchElementException("Reached end"); 
    } 

    return result; 

} 
0

next()を呼び出すたびに、結果を処理する必要があります。

next()メソッドの最初の行は、next()メソッドの最後にcurrent.next()を呼び出すため、最初の要素をスキップします。

もっと一般的に言えば、このコードはコレクションを処理する正しい方法ではありません。あなたは、使用時に問題を分析する必要があります。

0

問題は、あなたが(次回呼び出すたびに)あなたがそうずつ

current = values.next(); 

で始めることであるあなたが現在に反復を継続しようとせずに、次のイテレータにスキップ呼び出します。

代わりにあなたがのJava-8を使用している場合は、1次元配列にあなたの2D配列を鉄にflatMapToInt機能を利用することができ

if(!current.hasNext()) 
    current = values.next(); 
1
public static class IteratorNext { 

    private Iterator<Iterator<Integer>> values = null; 
    private Iterator<Integer> current; 

    public IteratorNext(Iterator<Iterator<Integer>> iterator) { 
     this.values = iterator; 
    } 

    public int next() throws NoSuchElementException { 

     if (current != null && current.hasNext()) { 
      Integer val = current.next(); 
      return val; 
     } 

     if (values != null && values.hasNext()) { 
      current = values.next(); 
      if (current != null && current.hasNext()) { 
       Integer val = current.next(); 
       return val; 
      } 
     } 

     throw new NoSuchElementException("Reached end"); 

    } 
} 
+0

第3の 'if'条件の' current'の 'null'チェックは、' current'が決して 'null'にならない' Iterator'を表しているので、実際には必要ありません。 – CKing

+1

あなたの変更内容を説明していただければ幸いです。 – andih

関連する問題