Advertisement
Guest User

Untitled

a guest
Apr 4th, 2018
128
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.08 KB | None | 0 0
  1. package arraylist.practice;
  2. public abstract class ArrayListClass<T>
  3. implements ArrayListADT<T>, Cloneable
  4. {
  5. protected int length; //to store the length of the list
  6. protected int maxSize; //to store the maximum size of
  7. //the list
  8. protected T[] list; //array to hold the list elements
  9.  
  10.  
  11. //Default constructor
  12. //Creates an array of size 100
  13. //Postcondition: list points to the array, length = 0,
  14. // and maxSize = 100
  15. public ArrayListClass()
  16. {
  17. maxSize = 100;
  18.  
  19. length = 0;
  20. list = (T[]) new Object[maxSize];
  21. }
  22.  
  23. //Constructor with a parameter
  24. //Creates an array of the size specified by the
  25. //parameter size.
  26. //Postcondition: list points to the array, length = 0,
  27. // and maxSize = size
  28. public ArrayListClass(int size)
  29. {
  30. if (size <= 0)
  31. {
  32. System.err.println("The array size must be positive. "
  33. + "Creating an array of size 100. ");
  34. maxSize = 100;
  35. }
  36. else
  37. maxSize = size;
  38.  
  39. length = 0;
  40. list = (T[]) new Object[maxSize];
  41. }
  42.  
  43. //Method to determine whether the list is empty.
  44. //Postcondition: Returns true if the list is empty;
  45. // otherwise, returns false.
  46. public boolean isEmpty()
  47. {
  48. return (length == 0);
  49. }
  50.  
  51. //Method to determine whether the list is full.
  52. //Postcondition: Returns true if the list is full;
  53. // otherwise, returns false.
  54. public boolean isFull()
  55. {
  56. return (length == maxSize);
  57. }
  58.  
  59. //Method to return the number of elements in the list.
  60. //Postcondition: Returns the value of length.
  61. public int listSize()
  62. {
  63. return length;
  64. }
  65.  
  66. //Method to return the maximum size of the list.
  67. //Postcondition: Returns the value of maxSize.
  68. public int maxListSize()
  69. {
  70. return maxSize;
  71. }
  72.  
  73. //Method to output the elements of the list.
  74. //Postcondition: Elements of the list are output on the
  75. //standard output device.
  76. public void print()
  77. {
  78. for (int i = 0; i < length; i++)
  79. System.out.println(list[i]);
  80. System.out.println();
  81. }
  82.  
  83. //Returns a copy of objects data in store.
  84. //This method clones only the references stored in
  85. //the array. The objects that the array references
  86. //point to are not cloned.
  87. public Object clone()
  88. {
  89. ArrayListClass<T> copy = null;
  90.  
  91. try
  92. {
  93. copy = (ArrayListClass<T>) super.clone();
  94. }
  95. catch (CloneNotSupportedException e)
  96. {
  97. return null;
  98. }
  99.  
  100. copy.list = (T[]) list.clone();
  101.  
  102. return copy;
  103. }
  104.  
  105. //Method to determine whether item is the same as the item
  106. //in the list at the position specified by location.
  107. //Postcondition: Returns true if list[location] is the
  108. // same as item; otherwise, returns false.
  109. public boolean isItemAtEqual(int location, T item)
  110. {
  111. if (location < 0 || location >= length)
  112. {
  113. System.err.println("The location of the item to "
  114. + "be compared is out of range.");
  115. return false;
  116. }
  117.  
  118. return (list[location].equals(item));
  119. }
  120.  
  121. //Method to remove all the elements from the list.
  122. //Postcondition: length = 0
  123. public void clearList()
  124. {
  125. for (int i = 0; i < length; i++)
  126. list[i] = null;
  127.  
  128. length = 0;
  129.  
  130. System.gc(); //invoke garbage collector
  131. } //end clearList
  132.  
  133. //Method to remove the item from the list at the position
  134. //specified by location.
  135. //Postcondition: The list element at list[location] is
  136. // removed and length is decremented by 1.
  137. // If location is out of range, an
  138. // appropriate message is output.
  139. public void removeAt(int location)
  140. {
  141. if (location < 0 || location >= length)
  142. System.err.println("The location of the item to "
  143. + "be removed is out of range.");
  144. else
  145. {
  146. for (int i = location; i < length - 1; i++)
  147. list[i] = list[i + 1];
  148.  
  149. list[length - 1] = null;
  150.  
  151. length--;
  152. }
  153. } //end removeAt
  154.  
  155. //Method to retrieve the element from the list at the
  156. //position specified by location.
  157. //Postcondition: A reference of the element at the
  158. // position specified by location is
  159. // returned. If location is out of range,
  160. // an appropriate message is output and
  161. // null is returned.
  162. public T retrieveAt(int location)
  163. {
  164. if (location < 0 || location >= length)
  165. {
  166. System.err.println("The location of the item to be "
  167. + "retrieved is out of range.");
  168. return null;
  169. }
  170. else
  171. return list[location];
  172. } //end retrieveAt
  173.  
  174.  
  175. //Method to insert insertItem in the list at the position
  176. //specified by location.
  177. //Postcondition: Starting at location, the elements of
  178. // the list are shifted to make room for
  179. // insertItem, list[location] = insertItem;,
  180. // and length++;
  181. // If the list is full or location is out
  182. // of range, an appropriate message is
  183. // output.
  184. public abstract void insertAt(int location, T insertItem);
  185.  
  186. //Method to insert insertItem at the end of the list.
  187. //Postcondition: list[length] = insertItem; and length++;
  188. // If the list is full, an appropriate
  189. // message is output.
  190. public abstract void insertEnd(T insertItem);
  191.  
  192. //Method to replace the element in the list at
  193. //the position specified by location with repItem.
  194. //Postcondition: list[location] = repItem
  195. // If location is out of range, an
  196. // appropriate message is output.
  197. public abstract void replaceAt(int location, T repItem);
  198.  
  199. //Method to determine whether searchItem is in the list.
  200. //Postcondition: If searchItem is found, returns the
  201. // location in the array where searchItem
  202. // is found; otherwise, returns -1.
  203. public abstract int seqSearch(T searchItem);
  204.  
  205. //Method to remove an item from the list.
  206. //The parameter removeItem specifies the item to
  207. //be removed.
  208. //Postcondition: If removeItem is found in the list, it
  209. // is removed from the list and length is
  210. // decremented by one.
  211. public abstract void remove(T removeItem);
  212. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement