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

4
  • 1
    you cannot increase array size, it has a fixed length Commented Mar 31, 2016 at 11:09
  • 1
    I suggest thinking about Lists Commented Mar 31, 2016 at 11:10
  • you cant increase an array in java, use Lists instead. Commented Mar 31, 2016 at 11:12
  • 1
    Possible duplicate of Variable length (Dynamic) Arrays in Java Commented Mar 31, 2016 at 11:18

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.