GenericListVersion2.java 6.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. import java.util.Collection;
  2. import java.util.Iterator;
  3. /**
  4. * A class to provide a simple list.
  5. * List resizes automatically. Used to illustrate
  6. * various design and implementation details of
  7. * a class in Java.
  8. *
  9. * @author scottm
  10. *
  11. */
  12. public class GenericListVersion2 implements Iterable{
  13. // class constant for default size
  14. private static final int DEFAULT_CAP = 10;
  15. //instance variables
  16. // iValues store the elements of the list and
  17. // may have extra capacity
  18. private Object[] iValues;
  19. private int iSize;
  20. private class GenericListIterator implements Iterator{
  21. private int position;
  22. private boolean removeOK;
  23. private GenericListIterator(){
  24. position = 0;
  25. removeOK = false;
  26. }
  27. public boolean hasNext(){
  28. return position < iSize;
  29. }
  30. public Object next(){
  31. Object result = iValues[position];
  32. position++;
  33. removeOK = true;
  34. return result;
  35. }
  36. public void remove(){
  37. if( !removeOK )
  38. throw new IllegalStateException();
  39. // which element should be removed??
  40. removeOK = false;
  41. GenericListVersion2.this.remove(position - 1);
  42. position--;
  43. }
  44. }
  45. public Iterator iterator(){
  46. return new GenericListIterator();
  47. }
  48. public void addAll(Collection c){
  49. // for each loop
  50. for(Object obj : c){
  51. this.add(obj);
  52. }
  53. }
  54. /**
  55. * Default add method. Add x to the end of this IntList.
  56. * Size of the list goes up by 1.
  57. * @param x The value to add to the end of this list.
  58. */
  59. public void add(Object x){
  60. insert(iSize, x);
  61. }
  62. public Object get(int pos){
  63. return iValues[pos];
  64. }
  65. /**
  66. * Insert obj at position pos.
  67. * post: get(pos) = x, size() = old size() + 1
  68. * @param pos 0 <= pos <= size()
  69. * @param obj The element to add.
  70. */
  71. public void insert(int pos, Object obj){
  72. ensureCapcity();
  73. for(int i = iSize; i > pos; i--){
  74. iValues[i] = iValues[i - 1];
  75. }
  76. iValues[pos] = obj;
  77. iSize++;
  78. }
  79. public Object remove(int pos){
  80. Object removedValue = iValues[pos];
  81. for(int i = pos; i < iSize - 1; i++)
  82. iValues[i] = iValues[i + 1];
  83. iValues[iSize - 1] = null;
  84. iSize--;
  85. return removedValue;
  86. }
  87. private void ensureCapcity(){
  88. // is there extra capacity available?
  89. // if not, resize
  90. if(iSize == iValues.length)
  91. resize();
  92. }
  93. public int size(){
  94. return iSize;
  95. }
  96. // resize internal storage container by a factor of 2
  97. private void resize() {
  98. Object[] temp = new Object[iValues.length * 2];
  99. System.arraycopy(iValues, 0, temp, 0, iValues.length);
  100. iValues = temp;
  101. }
  102. /**
  103. * Return a String version of this list. Size and
  104. * elements included.
  105. */
  106. public String toString(){
  107. // we could make this more effecient by using a StringBuffer.
  108. // See alternative version
  109. String result = "size: " + iSize + ", elements: [";
  110. for(int i = 0; i < iSize - 1; i++)
  111. result += iValues[i].toString() + ", ";
  112. if(iSize > 0 )
  113. result += iValues[iSize - 1];
  114. result += "]";
  115. return result;
  116. }
  117. // Would not really have this and toString available
  118. // both included just for testing
  119. public String toStringUsingStringBuffer(){
  120. StringBuffer result = new StringBuffer();
  121. result.append( "size: " );
  122. result.append( iSize );
  123. result.append(", elements: [");
  124. for(int i = 0; i < iSize - 1; i++){
  125. result.append(iValues[i]);
  126. result.append(", ");
  127. }
  128. if( iSize > 0 )
  129. result.append(iValues[iSize - 1]);
  130. result.append("]");
  131. return result.toString();
  132. }
  133. /**
  134. * Default constructor. Creates an empty list.
  135. */
  136. public GenericListVersion2(){
  137. //redirect to single int constructor
  138. this(DEFAULT_CAP);
  139. //other statments could go here.
  140. }
  141. /**
  142. * Constructor to allow user of class to specify
  143. * initial capacity in case they intend to add a lot
  144. * of elements to new list. Creates an empty list.
  145. * @param initialCap > 0
  146. */
  147. public GenericListVersion2(int initialCap) {
  148. assert initialCap > 0 : "Violation of precondition. IntListVer1(int initialCap):"
  149. + "initialCap must be greater than 0. Value of initialCap: " + initialCap;
  150. iValues = new Object[initialCap];
  151. iSize = 0;
  152. }
  153. /**
  154. * Return true if this IntList is equal to other.<br>
  155. * pre: none
  156. * @param other The object to comapre to this
  157. * @return true if other is a non null, IntList object
  158. * that is the same size as this IntList and has the
  159. * same elements in the same order, false otherwise.
  160. */
  161. public boolean equals(Object other){
  162. boolean result;
  163. if(other == null)
  164. // we know this is not null so can't be equal
  165. result = false;
  166. else if(this == other)
  167. // quick check if this and other refer to same IntList object
  168. result = true;
  169. else if( this.getClass() != other.getClass() )
  170. // other is not an IntList they can't be equal
  171. result = false;
  172. else{
  173. // other is not null and refers to an IntList
  174. GenericListVersion2 otherList = (GenericListVersion2)other;
  175. result = this.size() == otherList.size();
  176. int i = 0;
  177. while(i < iSize && result){
  178. result = this.iValues[i].equals( otherList.iValues[i] );
  179. i++;
  180. }
  181. }
  182. return result;
  183. }
  184. }