Home > Back-end >  Array won't resize without copyOf()
Array won't resize without copyOf()

Time:09-28

I have a method that's supposed to grow the size of an array by double once it reaches its capacity

I was trying to find a way to resize it without Arrays.copyOf but rather creating an array that's double the size and storing the original array in the new resized array

works

this.capacity = this.capacity * 2;
this.theData = Arrays.copyOf(this.theData, this.capacity);

doesn't work rather it throws away the first index and replaces it with the new value, without resizing

double arrayResize[] = new double[this.capacity*2];
for (int i = 0; i<this.theData.length;i  ){
        arrayResize[i] = this.theData[i];
}

CodePudding user response:

You cannot resize an array in Java. Use ArrayList or LinkedList instead.

If you need to have a list for a primitive types and you don't want to wrap them into objects, take a look on Colt or Parallel Colt libraries.

  • Related