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