Most memory efficient way to grow an array in Java?

前端 未结 12 1277
陌清茗
陌清茗 2020-12-13 19:18

I\'m not too concerned about time efficiency (the operation will be rare), but rather about memory efficiency: Can I grow the array without temporarily having all th

12条回答
  •  旧时难觅i
    2020-12-13 20:01

    "Can I grow the array without temporarily having all the values twice?"

    Even if you copy the arrays, you're only going to have all the values once. Unless you call clone() on your values, they're passed by reference into the new array.

    If you already have your values in memory, the only additional memory expense when copying into a new array is allocating the new Object[] array, which doesn't take much memory at all, as it's just a list of pointers to value objects.

提交回复
热议问题