1 22 23 package com.sosnoski.util; 24 25 import java.util.Iterator ; 26 import java.util.NoSuchElementException ; 27 28 36 37 public class SparseArrayIterator implements Iterator 38 { 39 40 protected Object [] m_array; 41 42 43 protected int m_offset; 44 45 50 51 private SparseArrayIterator(Object [] array) { 52 m_array = array; 53 m_offset = -1; 54 advance(); 55 } 56 57 64 65 protected boolean advance() { 66 while (++m_offset < m_array.length) { 67 if (m_array[m_offset] != null) { 68 return true; 69 } 70 } 71 return false; 72 } 73 74 80 81 public boolean hasNext() { 82 return m_offset < m_array.length; 83 } 84 85 91 92 public Object next() { 93 if (m_offset < m_array.length) { 94 Object result = m_array[m_offset]; 95 advance(); 96 return result; 97 } else { 98 throw new NoSuchElementException (); 99 } 100 } 101 102 108 109 public void remove() { 110 throw new UnsupportedOperationException (); 111 } 112 113 120 121 public static Iterator buildIterator(Object [] array) { 122 if (array == null || array.length == 0) { 123 return ArrayRangeIterator.EMPTY_ITERATOR; 124 } else { 125 return new SparseArrayIterator(array); 126 } 127 } 128 } 129 | Popular Tags |