Extend the size of an array



This example shows how to extend the size of an array. Since arrays are static in size, they cannot be extended in the way collection objects can (for example a Vector).
Hence we need to create a new array, add the new data and copy data from the first array.
In this example we create an array with three names, then we create another array with the length of 5. We add two names to position 3 and 4 (which is really position 4 and 5 since the first element of an array has the index 0).
Then we use the arraycopy() method of the System class and specify that we want to transfer data from the first position in the names array (param 1 and 2), and we want to insert the data in the array “extended” (param 3) from the first position (param 4) up to the length of the array “names” (param 5).
Finally we print the elements of the extended array.

So the output will look like this when the code above is executed:







Search for more Java info on this site here:
Custom Search


Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">