Max_Leb

Untitled

Jun 23rd, 2022
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.92 KB | None | 0 0
  1. interface List<T> {
  2.     void add(T element);
  3.     void put(T element, int index);
  4.     void remove(int index);
  5.     int find(T element);
  6.     T get(int index);
  7. }
  8.  
  9. class ArrayList<T> implements List<T> {
  10.     private static final int DEFAULT_ARRAYLIST_SIZE = 10;
  11.     public int capacity = DEFAULT_ARRAYLIST_SIZE;
  12.     private T[] elementData;
  13.     int size = 0;
  14.     private int lastElement = 0;
  15.    
  16.     public ArrayList() {
  17.         elementData = (T[])new Object[DEFAULT_ARRAYLIST_SIZE];
  18.     }
  19.    
  20.     public ArrayList(int capacity) {
  21.     elementData =(T[])new Object[capacity];
  22.         this.capacity = capacity;
  23.     }
  24.    
  25.     @Override
  26.     public void add(T element) {
  27.         if (size < capacity) {
  28.             elementData[size++] = element;
  29.         } else {
  30.             T[] tmp = (T[])new Object[capacity];
  31.             for (int i = 0; i < capacity; ++i) {
  32.                 tmp[i] = elementData[i];
  33.             }
  34.             elementData = (T[])new Object[capacity * 2];
  35.             for (int i = 0; i < capacity; ++i){
  36.                 elementData[i] = tmp[i];
  37.             }
  38.             capacity = capacity * 2;
  39.             elementData[size++] = element;
  40.         }
  41.         lastElement = size;
  42.     }
  43.    
  44.     @Override
  45.     public void put(T element, int index) {
  46.        
  47.         if (index < capacity) {
  48.             if (elementData[index] == null) {
  49.                 size++;
  50.             }
  51.             elementData[index] = element;
  52.         } else {
  53.             T[] tmp = (T[])new Object[capacity];
  54.             for (int i = 0; i < capacity; ++i) {
  55.                 tmp[i] = elementData[i];
  56.             }
  57.             elementData = (T[])new Object[index * 2];
  58.             for (int i = 0; i < capacity; ++i) {
  59.                 elementData[i] = tmp[i];
  60.             }
  61.             capacity = index * 2;
  62.             elementData[index] = element;
  63.             lastElement = index + 1;
  64.             size++;
  65.         }
  66.     }
  67.    
  68.     @Override
  69.     public void remove(int index) {
  70.         if (index >= size || index < 0) {
  71.             throw new ArrayIndexOutOfBoundsException();
  72.         }
  73.         elementData[index] = null;
  74.         int i = index;
  75.         for (; i < size - 1; i++) {
  76.             elementData[i] = elementData[i + 1];
  77.         }
  78.         elementData[i] = null;
  79.         size--;
  80.         lastElement = size;
  81.     }
  82.    
  83.     @Override
  84.     public int find(T element) {
  85.         for (int i = 0; i < size; ++i) {
  86.             if (elementData[i].equals(element)) {
  87.                 return i;
  88.             }
  89.         }
  90.         return -1;
  91.     }
  92.    
  93.     @Override
  94.     public T get(int index) {
  95.         if (index >= size || index < 0) {
  96.             throw new ArrayIndexOutOfBoundsException();
  97.         }
  98.         return elementData[index];
  99.     }
  100.    
  101.     public void print() {
  102.         for (int i = 0; i < lastElement; ++i) {
  103.             System.out.print(elementData[i] + " ");
  104.         }
  105.         System.out.println();
  106.     }
  107. }
Advertisement
Add Comment
Please, Sign In to add comment