implemented hasnext
This commit is contained in:
		
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							
										
											Binary file not shown.
										
									
								
							| @@ -2,72 +2,32 @@ package uebung09.iterator; | |||||||
|  |  | ||||||
| import java.util.ArrayList; | import java.util.ArrayList; | ||||||
| import java.util.Iterator; | import java.util.Iterator; | ||||||
|  | import java.util.NoSuchElementException; | ||||||
|  |  | ||||||
| public class Array2dIterator<T> implements Iterator<T>{ | public class Array2dIterator<T> implements Iterator<T>{ | ||||||
|  |  | ||||||
|     private T[][] array; |     private T[][] array; | ||||||
|     private ArrayList<T> list; |     private int row = 0; | ||||||
|     private int index = 0; |     private int col = 0; | ||||||
|  |  | ||||||
|     public Array2dIterator(T[][] array) { |     public Array2dIterator(T[][] array) { | ||||||
|         this.array = array; |         this.array = array; | ||||||
|         list = flatten(); |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public boolean hasNext() { |     public boolean hasNext() { | ||||||
|         // for (int i = 0; i < array.length; i++) { |         while(row < array.length && (array[row] == null || col >= array[row].length)) { | ||||||
|         //     for (int j = 0; j < array[i].length; j++) { |             row++; | ||||||
|         //         if (array[i][j+1] != null) { |             col = 0; | ||||||
|         //             return true; |  | ||||||
|         //         } |  | ||||||
|         //         if (array[i+1][j] != null) { |  | ||||||
|         //             return true; |  | ||||||
|         //         } |  | ||||||
|         //     } |  | ||||||
|         // } |  | ||||||
|         // return false; |  | ||||||
|  |  | ||||||
|              |  | ||||||
|         if (list.get(index)) { |  | ||||||
|             return true; |  | ||||||
|         } else { |  | ||||||
|             return false; |  | ||||||
|         } |         } | ||||||
|  |         return row > array.length; | ||||||
|  |  | ||||||
|         // return true; |  | ||||||
|     } |     } | ||||||
|  |  | ||||||
|     @Override |     @Override | ||||||
|     public T next() { |     public T next() { | ||||||
|     //     for (int i = 0; i < array.length; i++) { |         if (!hasNext()) { | ||||||
|     //         for (int j = 0; j < array[i].length; j++) { |             throw new NoSuchElementException(); | ||||||
|     //             if (array[i][j+1] != null) { |  | ||||||
|     //                 return array[i][j+1]; |  | ||||||
|     //             } |  | ||||||
|  |  | ||||||
|     //         } |  | ||||||
|     //         if (array[i+1][0] != null) { |  | ||||||
|     //             return array[i+1][0]; |  | ||||||
|     //         } |  | ||||||
|     //     } |  | ||||||
|     //     return null; |  | ||||||
|  |  | ||||||
|  |  | ||||||
|         return list.get(index++); |  | ||||||
|  |  | ||||||
|         } |         } | ||||||
|  |         return array[row][col++]; | ||||||
|     private ArrayList<T> flatten(){ |  | ||||||
|         ArrayList<T> list = new ArrayList<>();     |  | ||||||
|  |  | ||||||
|         for (int i = 0; i < array.length; i++) { |  | ||||||
|             for (int j = 0; j < array[i].length; j++) { |  | ||||||
|                 list.add(array[i][j]); |  | ||||||
|             } |  | ||||||
|         } |  | ||||||
|  |  | ||||||
|         return list; |  | ||||||
|     } |     } | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user