Advertisement
Guest User

Untitled

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