Advertisement
Guest User

Untitled

a guest
Feb 27th, 2020
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.17 KB | None | 0 0
  1. import java.util.Scanner;
  2. public class exc2 {
  3.  
  4. private Object[] arr;
  5. private int count;
  6. private static final int INITIAL_CAPACITY = 4;
  7.  
  8. /** Initializes the array-based list – allocate memory **/
  9.  
  10. public exc2()
  11. {
  12. arr = new Object[INITIAL_CAPACITY];
  13. count = 0;
  14. }
  15.  
  16. /**
  17. * @return the actual list length
  18. */
  19.  
  20. public int getLength()
  21. {
  22. return count;
  23. }
  24.  
  25. /**
  26. * Adds element to the list
  27. * @param item - the element you want to add
  28. */
  29.  
  30. public void add(Object item)
  31. {
  32. add(count, item);
  33. }
  34.  
  35. /**
  36. * Inserts the specified element at given position in this list
  37. * @param index -
  38. * index at which the specified element is to be inserted
  39. * @param item -
  40. * element to be inserted
  41. * @throws IndexOutOfBoundsException
  42. */
  43. public void add(int index, Object item)
  44. {
  45.  
  46. if (index>count || index<0)
  47. {
  48. throw new IndexOutOfBoundsException("Invalid index: " + index);
  49. }
  50.  
  51. Object[] extendedArr = arr;
  52. if (count + 1 == arr.length)
  53. {
  54. extendedArr = new Object[arr.length * 2];
  55. }
  56.  
  57. System.arraycopy(arr, 0, extendedArr, 0, index);
  58. count++;
  59. System.arraycopy(arr, index, extendedArr, index+1, count-index-1);
  60. extendedArr[index] = item;
  61. arr = extendedArr;
  62. }
  63.  
  64. /**
  65. * Returns the index of the first occurrence of the specified
  66. * element in this list.
  67. *
  68. * @param item - the element you are searching
  69. * @return the index of given element or -1 is not found
  70. */
  71.  
  72. public int indexOf(Object item)
  73. {
  74. if (item == null) {
  75. for (int i = 0; i < arr.length; i++)
  76. {
  77. if (arr[i] == null) return i;
  78. }
  79. }
  80. else
  81. {
  82. for (int i = 0; i < arr.length; i++)
  83.  
  84. if (item.equals(arr[i])) return i;
  85. }
  86.  
  87. return -1;
  88. }
  89.  
  90. /**
  91. * Clears the list
  92. */
  93.  
  94. public void clear() {
  95. arr = new Object[0];
  96. count = 0;
  97. }
  98.  
  99. /**
  100. * Checks if an element exists
  101. * @param item – the item to be checked
  102. * @return if the item exists
  103. */
  104.  
  105. public boolean contains(Object item)
  106. {
  107. int index = indexOf(item);
  108. boolean found = (index != -1);
  109. return found;
  110. }
  111.  
  112. /**
  113. * @return the object on given position
  114. */
  115. public Object elementAt(int index)
  116. {
  117. if (index>=count || index<0)
  118. {
  119. throw new IndexOutOfBoundsException("Invalid index: " + index);
  120. }
  121. return arr[index];
  122. }
  123.  
  124. /**
  125. * Removes the element at the specified index
  126. * @param index - the index, whose element you want to remove
  127. * @return the removed element
  128. */
  129.  
  130. public Object remove(int index)
  131. {
  132. if (index>=count || index<0)
  133. {
  134. throw new IndexOutOfBoundsException("Invalid index: " + index);
  135. }
  136.  
  137. Object item = arr[index];
  138. System.arraycopy(arr, index+1, arr, index, count-index+1);
  139. arr[count - 1] = null;
  140. count--;
  141. return item;
  142. }
  143.  
  144.  
  145.  
  146. /**
  147. * Removes the specified item and returns its index or -1
  148. * if item does not exists
  149. * @param item - the item you want to remove
  150. */
  151.  
  152. public int remove(Object item)
  153. {
  154. int index = indexOf(item);
  155.  
  156. if (index == -1)
  157. {
  158. return index;
  159. }
  160. System.arraycopy(arr, index+1, arr, index, count-index+1);
  161. count--;
  162. return index;
  163. }
  164.  
  165.  
  166. public static void main(String[] args)
  167. {
  168.  
  169. exc2 shoppingList = new exc2 ();
  170. Scanner sc=new Scanner(System.in);
  171.  
  172. while(true)
  173. {
  174. System.out.println();
  175. System.out.println("For ADD choose 1, for REMOVE choose 2, for END choose 3");
  176. int choice=sc.nextInt();
  177.  
  178. if(choice==1)
  179. {
  180. System.out.println("The element you want to add:");
  181. String n=sc.next();
  182.  
  183. if(shoppingList.contains(n)==false)
  184. {
  185. shoppingList.add(n);
  186.  
  187. System.out.println();
  188. System.out.println("Now your list contains:");
  189. getList(shoppingList);
  190.  
  191. continue;
  192.  
  193. } else
  194. {
  195. System.out.println("The element is already in the list");
  196. continue;
  197. }
  198. }
  199.  
  200. if(choice==2)
  201. {
  202. System.out.println("The element you want to remove:");
  203. String n=sc.next();
  204.  
  205. if(shoppingList.contains(n))
  206. {
  207. shoppingList.remove(shoppingList.indexOf(n));
  208.  
  209. System.out.println();
  210. System.out.println("Now your list contains:");
  211. getList(shoppingList);
  212.  
  213. continue;
  214.  
  215. } else
  216. {
  217. System.out.println("No element to remove");
  218. continue;
  219. }
  220. }
  221.  
  222. if(choice==3)
  223. {
  224. System.out.println();
  225. System.out.println("Now your list contains:");
  226. getList(shoppingList);
  227.  
  228. System.out.println("Program ended");
  229. break;
  230. }
  231.  
  232.  
  233. } // end while
  234.  
  235. sc.close();
  236.  
  237. } // end MAIN
  238.  
  239.  
  240.  
  241. static void getList(exc2 shoppingList)
  242. {
  243. for(int i=0; i<shoppingList.getLength(); i++)
  244. {
  245. System.out.println(shoppingList.elementAt(i));
  246. }
  247. }
  248.  
  249. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement