0

I am having trouble trying to figure out how to increase size a of an array A so that A has a large enough buffer at the end to hold an array B.

Assuming the two sorted arrays are A = {1, 3, 5} and B = {2, 4, 6, 8}

A = {1, 3, 5, 0, 0, 0, 0} and B = {2, 4, 6, 8} where A has buffer {0, 0, 0, 0} at the end to hold B

Roman
  • 3,563
  • 5
  • 48
  • 104
Nikhilesh Sharma
  • 141
  • 1
  • 5
  • 16

2 Answers2

2

You cannot resize an array, you have to either construct a new array with the size of both arrays combined, or use another data structure (e.g. ArrayList).

Also see: Make copy of array in Java

Community
  • 1
  • 1
Dimitrios Begnis
  • 823
  • 6
  • 16
2

If you are using a literal array you have no chance of increasing its size in place. You must create a new array and copy A over it, perhaps by using System.arrayCopy.