I have a task to make my own Dynamic Array without using any ArrayLists or other pre-made classes for Arrays in java.
I get an Exception in thread "main" java.lang.NullPointerException error when I try to add my object (for example it's Integer).
Dinaminis<Integer> array = new Dinaminis<>();
array.ideti(5);
And my Dinaminis class looks like this:
public class Dinaminis<T> implements DArray<T>
{
private Object[] array;
private int kiek;
private Object[] temp;
public Dinaminis() {
array = new Object[10];
kiek = 0;
}
@Override
public void ideti(Object o) {
if (array.length == kiek) {
temp = new Object[kiek*2];
}
for (int i=1; i < kiek; i++){
temp[i] = array[i];
}
array = temp;
array[kiek] = o;
kiek++;
}
}
The thing is when I want to use "ideti" method, I want to check if the array is full, and if it's full it should create a double sized array. But I get an error even before my array is full.
array.length == kiekisfalse?tempan instance variable (just local to the method), andSystem.arraycopyis a lot faster than hand-copying.