Guest User

Untitled

a guest
Jan 6th, 2018
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.66 KB | None | 0 0
  1. public class ListOfStrings {
  2. public static final int DEFAULT_CAPACITY = 4;
  3. private String[] names;
  4. private int size = 0;
  5.  
  6. public ListOfStrings() {
  7. this(DEFAULT_CAPACITY);
  8. }
  9.  
  10. public ListOfStrings(int initialCapacity) {
  11. names = new String[initialCapacity];
  12. }
  13.  
  14. public int getCapacity() {
  15. return names.length;
  16. }
  17.  
  18. public int getSize() {
  19. return size;
  20. }
  21.  
  22. // Adds name to the end of the list
  23. public void add(String name) {
  24. if (this.getSize() == this.getCapacity()) {
  25. // Double the size of the array
  26. String[] temp = new String[this.getCapacity() * 2];
  27. System.arraycopy(names, 0, temp, 0, this.getCapacity());
  28. names = temp;
  29. }
  30.  
  31. names[size] = name;
  32. size++;
  33. }
  34.  
  35. // set item i to the given name
  36. public void set(int i, String name) {
  37. if (i < 0 || i > this.getSize() - 1) {
  38. // we have a problem
  39. String message = "index " + i + " not valid";
  40. throw new IndexOutOfBoundsException(message);
  41. }
  42.  
  43. names[i] = name;
  44. }
  45.  
  46. // returns the item at index i
  47. public String get(int i) {
  48. if (i < 0 || i > this.getSize() - 1) {
  49. // we have a problem
  50. String message = "index " + i + " not valid";
  51. throw new IndexOutOfBoundsException(message);
  52. }
  53.  
  54. return names[i];
  55. }
  56.  
  57. // removes and returns item i from the list
  58. public String remove(int i) {//
  59. if (i < 0 || i > this.getSize() - 1) {
  60. // we have a problem
  61. String message = "index " + i + " not valid";
  62. throw new IndexOutOfBoundsException(message);
  63. }
  64.  
  65. String removedItem = this.get(i); // save item to return
  66.  
  67. // now adjust the array
  68. if (i < size - 1) {
  69. System.arraycopy(names, i + 1, names, i, size - i - 1);
  70. }
  71. size--;
  72.  
  73. return removedItem;
  74. }
  75.  
  76. @Override
  77. public String toString() {
  78. String s = "";
  79. s += "[";
  80. boolean first = true;
  81. for (int i = 0; i < size; i++) {
  82. if (first) {
  83. s += names[i];
  84. first = false;
  85. } else {
  86. s += ", ";
  87. s += names[i];
  88. }
  89. }
  90. s += "]";
  91.  
  92. return s;
  93. }
  94.  
  95. /*
  96. * Removes and returns the first string in the list. If the list is empty,
  97. * null is returned.
  98. */
  99. public String removeFirst() {
  100. if (size < 1)
  101. return null;
  102. String removeItem = this.get(0);
  103.  
  104. this.remove(0);
  105. return removeItem;
  106. }
  107.  
  108. /*
  109. * Removes and returns the last string in the list. If the list is empty,
  110. * null is returned.
  111. */
  112. public String removeLast() {
  113. if (size < 1)
  114. return null;
  115. String removeItem = this.get(size - 1);
  116. this.remove(size - 1);
  117. size--;
  118.  
  119. return removeItem;
  120. }
  121.  
  122. /*
  123. * Removes all unused array elements from the end of the arry, if any exit.
  124. * After calling this method the size and the capacity of the list should be
  125. * same.
  126. */
  127. public void compress() {
  128. if (size == DEFAULT_CAPACITY)
  129. return;
  130. if (size == 0) {
  131. this.names = new String[] {};
  132. return;
  133. }
  134.  
  135. String[] newList = new String[size];
  136. System.arraycopy(names, 0, newList, 0, size);
  137. this.names = newList;
  138.  
  139. }
  140.  
  141. /*
  142. * Increases the capacity, if needed, to new specified capacity. This may
  143. * mean making a new, lager array.
  144. */
  145. public void ensureCapacity(int newCapacity) {
  146.  
  147. if (newCapacity <this.getCapacity()) {
  148.  
  149. String message = "The new capacity must be lager than the original capacity("
  150. + this.getCapacity() + ").";
  151. throw new IndexOutOfBoundsException(message);
  152.  
  153. }
  154.  
  155. String[] newList = new String[newCapacity];
  156.  
  157. if (size > 0)
  158. System.arraycopy(names, 0, newList, 0, size);
  159.  
  160. this.names = newList;
  161.  
  162. }
  163.  
  164. /*
  165. * Returns the index of the first occurrence of specified string. If the
  166. * string is not in the list, returns -1.
  167. */
  168. public int getIndex(String s) {
  169. if (size == 0) {
  170. return -1;
  171.  
  172. }
  173. for (int i = 0; i < size; i++) {
  174. if (get(i).equals(s)) {
  175. return i;
  176. }
  177. }
  178. return -1;
  179. }
  180.  
  181. /*
  182. * Removes and returns the first occurrence of the specified string. If the
  183. * String is not in the list, returns null;
  184. */
  185. public String remove(String s) {
  186.  
  187. for (int i = 0; i < size; i++) {
  188. if (get(i).equals(s)) {
  189. this.remove(i);
  190. return s;
  191. }
  192. }
  193. return null;
  194. }
  195.  
  196. /* Removes all strings from the list and set the capacity to the default capacity.
  197. *
  198. * */
  199. public void Clear() {
  200. names = new String[DEFAULT_CAPACITY];
  201. size = 0;
  202. }
  203. }
  204.  
  205. @Override
  206. public String toString() {
  207. if (size < 1)
  208. return "[]";
  209. StringBuilder readable = new StringBuilder(size * 7);
  210. String intro = "[";
  211. for (int i = 0; i < size; i++) { // T item: names
  212. readable.append(intro).append(names[i]);
  213. intro = ", ";
  214. }
  215. return readable.append(']').toString();
  216. }
Add Comment
Please, Sign In to add comment