Resizing an Array

前端 未结 2 502

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:

相关标签:
2条回答
  • 2021-01-13 07:12

    Two things:

    1. If you're getting a null pointer on the first line of resize() it's probably because you have no value in myArray[0]. If that is null, your getClass() will bomb.

    2. You don't appear to be assigning myArray = tempArray so you'll probably run into an ArrayIndexOutOfBounds soon.

    0 讨论(0)
  • 2021-01-13 07:32

    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()

    0 讨论(0)
提交回复
热议问题