как построить конструктор динамического массива?
Подскажите, как построить конструктор динамического массива? Ошибки Type parameter T cannot be instantiated directly / expected По ссылке нахожу https://stackoverflow.com/questions/299998/instantiating-object-of-type-parameter Неужели все настолько сложно?
public static class DynamicArray<T> {
private T[] myArray;
private int size = 0;
private int capacity = 2;
Object[] array = new Object[10];
public DynamicArray(T) {
this.myArray = new Object[T];
size = 0;
capacity = 2;
}
public void add(T el) {
if (size == capacity) {
T[] tempArray = new T[(capacity * 2)]; //создаем новый массив большего размера
System.arraycopy(myArray, 0, tempArray, 0, capacity); //копируем в новый массив элементы из старого массива
myArray = tempArray;
capacity = capacity * 2;
}
myArray[size] = (T) el;
size++;
}
public void remove(int index) {
if (index >= size || index < 0) {
throw new IndexOutOfBoundsException("Element can't be found! "
+ "Number of elements in array = " + size
+ ". Total size of array = " + capacity);
}
T[] temp = myArray;
myArray = new T[temp.length - 1];
T value = temp[index];
System.arraycopy(temp, 0, myArray, 0, index); //копируем левую часть массива до указанного индекса
System.arraycopy(temp, index + 1, myArray, index, temp.length - index - 1); //копируем правую часть массива после указанного индекса
size--;
}
public T get(int index) {
if (index >= capacity | 0 > index) {
throw new ArrayIndexOutOfBoundsException();
} else {
return (T) array[index];
}
}
}