Advertisement
Guest User

Untitled

a guest
Mar 30th, 2015
261
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.58 KB | None | 0 0
  1. public ArrayList(Object[] array) {
  2.         _initialCapacity = array.length;
  3.         clear();
  4.         System.arraycopy(array, 0, _array, 0, array.length);
  5.         _size = array.length;
  6.     }
  7.  
  8.     //jeśli trzeba powiększ tablicę; value nie może być null
  9.     public void insert(int index, Object value) throws IndexOutOfBoundsException {
  10.         if(index<0 || index>_size) throw new IndexOutOfBoundException();
  11.         ensureCapacity(_size + 1);
  12.         System.arraycopy(_array, index, _array, index + 1, _size - index);
  13.         _array[index] = value;
  14.         ++_size;
  15.     }
  16.  
  17.     public Object delete(int index) throws IndexOutOfBoundsException {
  18.         checkOutOfBounds(index);
  19.         Object value = _array[index];
  20.         int copyFrom = index + 1;
  21.         if (copyFrom < _size)
  22.             System.arraycopy(_array, copyFrom, _array, index, _size - copyFrom);
  23.         --_size;
  24.         return value;
  25.     }
  26.  
  27.     public boolean delete(Object value) {
  28.         int index = indexOf(value);
  29.         if (index != -1)
  30.             delete(index);
  31.         return index != -1;
  32.     }
  33.  
  34.     public void add(Object value) {
  35.         insert(size(), value);
  36.     }
  37.  
  38.     public boolean contains(Object value) {
  39.         return indexOf(value) != -1;
  40.     }
  41.  
  42.     public boolean isEmpty() {
  43.         return _size == 0;
  44.     }
  45.  
  46.     public void clear() {
  47.         _array = new Object[_initialCapacity];
  48.         _size = 0;
  49.     }
  50.     public Object set(int index, Object value) throws IndexOutOfBoundsException {
  51.         checkOutOfBounds(index);
  52.         Object oldValue = _array[index];
  53.         _array[index] = value;
  54.         return oldValue;
  55.     }
  56.  
  57.     public Object get(int index) throws IndexOutOfBoundsException {
  58.         checkOutOfBounds(index);
  59.         return _array[index];
  60.     }
  61.  
  62.     public int indexOf(Object value) {
  63.         int i =0;
  64.         while(i < _size && value.equals(_array[i]))
  65.               ++i;
  66.         return i<_size ? i : -1;
  67.     }
  68.  
  69.     public Iterator iterator() {
  70.         return new ArrayIterator(_array, 0, _size);
  71.     }
  72.  
  73.     public int size() {
  74.         return _size;
  75.     }
  76.    
  77. // wykorzystywana przy wstawianiu;
  78.     private void ensureCapacity(int capacity) {
  79.           if (_array.length < capacity) {
  80.             Object[] copy = new Object[capacity + capacity / 2];
  81.             System.arraycopy(_array, 0, copy, 0, _size);
  82.             _array = copy;
  83.         }
  84.     }
  85.  
  86.     private void checkOutOfBounds(int index) throws IndexOutOfBoundsException {
  87.         if (index < 0 || index >= size())
  88.             throw new IndexOutOfBoundsException();
  89.         }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement