Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing an Array

If this were a regular array, I could just create a new array and then do arraycopy, but generics won't let me do that. The best thing I've come up with so far is:

public void resize() {
    T[] tempArray = Arrays.copyOf(myArray,myArray.length*3);
}

It compiles, but at run time, I get a null pointer exception. Can anyone explain what I'm doing wrong?

like image 370
MatthewK Avatar asked Sep 05 '11 23:09

MatthewK


1 Answers

you can use Arrays.copyOf(myArray,myArray.length*3) to make the copy

my guess is that myArray[0] is null so myArray[0].getClass() throws the nullpointer

if you need the runtime type of the components you can use myArray.getClass().getComponentType()

like image 171
ratchet freak Avatar answered Oct 07 '22 05:10

ratchet freak