Advertisement
Mariyan17320

Iliev's homework-23_1-Methods

Apr 30th, 2020
341
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.31 KB | None | 0 0
  1. public class iliev {
  2.     private Object[] arr;
  3.     private int count;
  4.     private static final int INITIAL_CAPACITY = 10;
  5.  
  6.     public iliev() {
  7.         arr = new Object[INITIAL_CAPACITY];
  8.         count = 0;
  9.     }
  10.  
  11.     public int getLength() {
  12.         return count;
  13.     }
  14.  
  15.     public void add(Object item) {
  16.         add(count, item);
  17.     }
  18.  
  19.     public void add(int index, Object item) {
  20.         if (index > count || index < 0) {
  21.             throw new IndexOutOfBoundsException("Invalid index: " + index);
  22.         }
  23.         Object[] extendedArr = arr;
  24.         if (count + 1 == arr.length) {
  25.             extendedArr = new Object[arr.length * 2];
  26.         }
  27.         System.arraycopy(arr, 0, extendedArr, 0, index);
  28.         count++;
  29.         System.arraycopy(arr, index, extendedArr, index + 1, count - index - 1);
  30.         extendedArr[index] = item;
  31.         arr = extendedArr;
  32.     }
  33.  
  34.     public int indexOf(Object item) {
  35.         if (item == null) {
  36.             for (int i = 0; i < arr.length; i++) {
  37.                 if (arr[i] == null)
  38.                     return i;
  39.             }
  40.         } else {
  41.             for (int i = 0; i < arr.length; i++)
  42.                 if (item.equals(arr[i]))
  43.                     return i;
  44.         }
  45.         return -1;
  46.     }
  47.  
  48.     public void clear() {
  49.         arr = new Object[0];
  50.         count = 0;
  51.     }
  52.  
  53.     public boolean contains(Object item) {
  54.         int index = indexOf(item);
  55.         boolean found = (index != -1);
  56.         return found;
  57.     }
  58.  
  59.     public Object elementAt(int index) {
  60.         if (index >= count || index < 0) {
  61.             throw new IndexOutOfBoundsException("Invalid index: " + index);
  62.         }
  63.         return arr[index];
  64.     }
  65.  
  66.     public Object remove(int index) {
  67.         if (index >= count || index < 0) {
  68.             throw new IndexOutOfBoundsException("Invalid index: " + index);
  69.         }
  70.         Object item = arr[index];
  71.         System.arraycopy(arr, index + 1, arr, index, count - index + 1);
  72.         arr[count - 1] = null;
  73.         count--;
  74.         return item;
  75.     }
  76.  
  77.     public int remove(Object item) {
  78.         int index = indexOf(item);
  79.         if (index == -1) {
  80.             return index;
  81.         }
  82.         System.arraycopy(arr, index + 1, arr, index, count - index + 1);
  83.         count--;
  84.         return index;
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement