Advertisement
Guest User

Untitled

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