Advertisement
Xugpaa

Untitled

Feb 27th, 2020
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.72 KB | None | 0 0
  1. package calculator;
  2. import java.util.Scanner;
  3.  
  4. public class CustomArrayList {
  5. private Object[] arr;
  6. private int count;
  7. private static final int INITIAL_CAPACITY = 4;
  8.  
  9. /** Initializes the array-based list – allocate memory **/
  10.  
  11. public CustomArrayList() {
  12. arr = new Object[INITIAL_CAPACITY];
  13. count = 0;
  14. }
  15. /**
  16. * @return the actual list length
  17. */
  18.  
  19. public int getLength() {
  20. return count;
  21. }
  22.  
  23. /**
  24. * Adds element to the list
  25. * @param item - the element you want to add
  26. */
  27.  
  28. public void add(Object item) {
  29. add(count, item);
  30. }
  31.  
  32. /**
  33. * Inserts the specified element at given position in this list
  34. * @param index - index at which the specified element is to be inserted
  35. * @param item - element to be inserted
  36. * @throws IndexOutOfBoundsException
  37. */
  38.  
  39. public void add(int index, Object item) {
  40. if (index>count || index<0) {
  41. throw new IndexOutOfBoundsException(
  42. "Invalid index: " + index);
  43. }
  44.  
  45. Object[] extendedArr = arr;
  46. if (count + 1 == arr.length) {
  47. extendedArr = new Object[arr.length * 2];
  48. }
  49.  
  50. System.arraycopy(arr, 0, extendedArr, 0, index);
  51. count++;
  52. System.arraycopy(
  53. arr, index, extendedArr, index+1, count-index-1);
  54. extendedArr[index] = item;
  55. arr = extendedArr;
  56. }
  57.  
  58. /**
  59. * Returns the index of the first occurrence of the specified element in this list.
  60. * @param item - the element you are searching
  61. * @return the index of given element or -1 is not found
  62. */
  63.  
  64. public int indexOf(Object item) {
  65. if (item == null) {
  66. for (int i = 0; i < arr.length; i++) {
  67. if (arr[i] == null)
  68. return i;
  69. }
  70. } else {
  71. for (int i = 0; i < arr.length; i++)
  72. if (item.equals(arr[i]))
  73. return i;
  74. }
  75. return -1;
  76. }
  77.  
  78. /**
  79. * Clears the list
  80. */
  81.  
  82. public void clear() {
  83. arr = new Object[0];
  84. count = 0;
  85. }
  86.  
  87. /**
  88. * Checks if an element exists
  89. * @param item – the item to be checked
  90. * @return if the item exists
  91. */
  92.  
  93. public boolean contains(Object item) {
  94. int index = indexOf(item);
  95. boolean found = (index != -1);
  96. return found;
  97. }
  98.  
  99. /**
  100. * @return the object on given position
  101. */
  102.  
  103. public Object elementAt(int index) {
  104. if (index>=count || index<0) {
  105. throw new IndexOutOfBoundsException(
  106. "Invalid index: " + index);
  107. }
  108. return arr[index];
  109. }
  110. /**
  111. * Removes the element at the specified index
  112. * @param index - the index, whose element you want to remove
  113. * @return the removed element
  114. */
  115.  
  116. public Object remove(int index) {
  117. if (index>=count || index<0) {
  118. throw new IndexOutOfBoundsException(
  119. "Invalid index: " + index);
  120. }
  121.  
  122. Object item = arr[index];
  123. System.arraycopy(arr, index+1, arr, index, count-index+1);
  124. arr[count - 1] = null;
  125. count--;
  126. return item;
  127. }
  128.  
  129. /**
  130. * Removes the specified item and returns its index or -1
  131. * if item does not exists
  132. * @param item - the item you want to remove
  133. */
  134.  
  135. public int remove(Object item) {
  136. int index = indexOf(item);
  137. if (index == -1) {
  138. return index;
  139. }
  140.  
  141. System.arraycopy(arr, index+1, arr, index, count-index+1);
  142. count--;
  143. return index;
  144.  
  145. }
  146. public static void main(String[] args){
  147.  
  148. Scanner scan = new Scanner(System.in);
  149.  
  150. CustomArrayList shoppingList = new CustomArrayList();
  151. shoppingList.add("Milk");
  152. shoppingList.add("Honey");
  153. shoppingList.add("Olives");
  154. shoppingList.add("Beer");
  155. shoppingList.remove("Olives");
  156. while(true) {
  157. System.out.println("What do you want to do?\n 1.Add Element \n 2.Remove Element \n 3.End Program");
  158. String question = scan.nextLine().toLowerCase();
  159.  
  160. if(question.equals("add")){
  161. String product = scan.nextLine();
  162. if(shoppingList.contains(product)) {
  163. System.out.println("This element is alreadt in");
  164. }else{
  165. System.out.println("add element");
  166. shoppingList.add(product);
  167. }
  168. }
  169. else if(question.equals("remove")) {
  170. String product = scan.nextLine();
  171. if(shoppingList.contains(product)) {
  172. System.out.println("remove element");
  173. shoppingList.remove(product);
  174. }else{
  175. System.out.println("This element is missing ");
  176.  
  177. }
  178. }
  179. else if(question.equals("end")) {
  180. System.out.println("the program is off");
  181. break;
  182. }else{
  183. System.out.println("wrong command");
  184. }
  185. for(int i=0; i<shoppingList.getLength(); i++) {
  186. System.out.println(shoppingList.elementAt(i));
  187. }
  188. }
  189.  
  190. }
  191. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement