123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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 1 only contains the instance variables and
  8. * the constructors
  9. * @author scottm
  10. *
  11. */
  12. public class IntListVer2{
  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 add method. Add x to the end of this IntList.
  22. * Size of the list goes up by 1.
  23. * @param x The value to add to the end of this list.
  24. */
  25. public void add(int x){
  26. // is there extra capacity available?
  27. // if not, resize
  28. if(iSize == iValues.length)
  29. resize();
  30. assert 0 <= iSize && iSize < iValues.length;
  31. iValues[iSize] = x;
  32. iSize++;
  33. }
  34. // resize internal storage container by a factor of 2
  35. private void resize() {
  36. int[] temp = new int[iValues.length * 2];
  37. System.arraycopy(iValues, 0, temp, 0, iValues.length);
  38. iValues = temp;
  39. }
  40. /**
  41. * Return a String version of this list. Size and
  42. * elements included.
  43. */
  44. public String toString(){
  45. // we could make this more effecient by using a StringBuffer.
  46. // See alternative version
  47. String result = "size: " + iSize + ", elements: [";
  48. for(int i = 0; i < iSize - 1; i++)
  49. result += iValues[i] + ", ";
  50. if(iSize > 0 )
  51. result += iValues[iSize - 1];
  52. result += "]";
  53. return result;
  54. }
  55. // Would not really have this and toString available
  56. // both included just for testing
  57. public String toStringUsingStringBuffer(){
  58. StringBuffer result = new StringBuffer();
  59. result.append( "size: " );
  60. result.append( iSize );
  61. result.append(", elements: [");
  62. for(int i = 0; i < iSize - 1; i++){
  63. result.append(iValues[i]);
  64. result.append(", ");
  65. }
  66. if( iSize > 0 )
  67. result.append(iValues[iSize - 1]);
  68. result.append("]");
  69. return result.toString();
  70. }
  71. /**
  72. * Default constructor. Creates an empty list.
  73. */
  74. public IntListVer2(){
  75. //redirect to single int constructor
  76. this(DEFAULT_CAP);
  77. //other statments could go here.
  78. }
  79. /**
  80. * Constructor to allow user of class to specify
  81. * initial capacity in case they intend to add a lot
  82. * of elements to new list. Creates an empty list.
  83. * @param initialCap > 0
  84. */
  85. public IntListVer2(int initialCap) {
  86. assert initialCap > 0 : "Violation of precondition. IntListVer1(int initialCap):"
  87. + "initialCap must be greater than 0. Value of initialCap: " + initialCap;
  88. iValues = new int[initialCap];
  89. iSize = 0;
  90. }
  91. /**
  92. * Return true if this IntList is equal to other.<br>
  93. * pre: none
  94. * @param other The object to comapre to this
  95. * @return true if other is a non null, IntList object
  96. * that is the same size as this IntList and has the
  97. * same elements in the same order, false otherwise.
  98. */
  99. public boolean equals(Object other){
  100. boolean result;
  101. if(other == null)
  102. // we know this is not null so can't be equal
  103. result = false;
  104. else if(this == other)
  105. // quick check if this and other refer to same IntList object
  106. result = true;
  107. else if( this.getClass() != other.getClass() )
  108. // other is not an IntList they can't be equal
  109. result = false;
  110. else{
  111. // other ris not null and refers to an IntList
  112. IntListVer2 otherIntList = (IntListVer2)other;
  113. result = this.iSize == otherIntList.iSize;
  114. int i = 0;
  115. while(i < iSize && result){
  116. result = this.iValues[i] == otherIntList.iValues[i];
  117. i++;
  118. }
  119. }
  120. return result;
  121. }
  122. }