2

In C/C++ we have realloc which will efficiently allocate additional space for an existing collection. I guess it is sub linear (or even constant) in complexity.

Is there a way to achieve the same in Java? Here are the items that I looked at,

  1. Array resize is not possible,
  2. Copying an array to another of bigger size is linear in complexity. Looked at both System.arrayCopy as well as Arrays.copyOf
  3. ArrayList must be same as point 2 above.

Note : My requirement is to expand possibly an extremely large array to even further.

4
  • I think no, but you can get closer to it by using some factors like amortized time and using system methods. In this case System.arrayCopy is much faster. Commented Mar 22, 2014 at 4:31
  • 2
    Don't even worry about it in Java, frankly. Commented Mar 22, 2014 at 4:37
  • 2
    Must it really be one large array? Unless you are interfacing it with something else (jni) I'd probably use a different structure--a linked list of arrays or something? You can still wrap it in a class that lets it interact with it however you like--that is all assuming you are talking an array more than, say, 1/3 the ram of your PC, if it's not that big I'd just do what Louis said, don't worry about it and let java handle it for you, Java will do very smart things in general. Commented Mar 22, 2014 at 4:40
  • @BillK, & louis, I get the general picture. Thank you for the inputs. Commented Mar 22, 2014 at 4:56

1 Answer 1

5

realloc is likely to be O(n) in practice, since it sometimes/often involves memory copying. In that sense, it's equivalent in theoretical complexity to allocating a new array in Java.

Now Java always zeros the newly allocated memory which may take it a bit longer, but OTOH the GC has insanely fast memory allocations so it may even be faster than realloc in some cases. I'd expect a strategy that involves allocating new arrays in Java to be overall roughly comparable in speed to realloc. Possibly Java is better for smaller arrays, C/C++ would have the edge for big arrays, but YMMV. You'd have to benchmark in your particular implementation and workload to be sure.

So overall:

  • Don't worry about it, just reallocate new arrays in Java
  • If you do this a lot, be sure to recreate arrays with more space than you need so that you don't need to reallocate with each single element added (this is what the Java ArrayList does internally.

Final but important point: unless you are writing very low level code, you probably shouldn't be worrying about this anyway. Just use one of the fine collection classes that already exist (Java Collections, Google Collections, Trove etc.) and let them handle all of this stuff for you.

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

2 Comments

Based on all your inputs, I get the idea. Will try with Arrays.copyTo and see how it goes. Thank you.

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.