Current location - Plastic Surgery and Aesthetics Network - Plastic surgery and beauty - How does java add an index to an array that exceeds the length of the array?
How does java add an index to an array that exceeds the length of the array?
This is generally done. When the access is out of bounds, or the added data exceeds the current length of the array.

Reapply an array greater than or equal to the out-of-bounds length.

Then copy the contents of the original array to the new memory block through Sysmem.arraycopy.

If some elements are deleted, the capacity (container size) will be much larger than the actual length. You can rebuild the trimmed array and copy it.

This is also the implementation method in ArrayList. System.arraycopy is a native method with high replication efficiency.

Or you can look at the source code of arraylist.