Question: I want to allocate an arraylist of primitive datatype objects like int,char etc (not array because it's going to grow in future may be not linearly) but required to be stored at consecutive memory locations in RAM (jvm managed heap). But since when I declare ArrayList for primitive data type for ex. ArrayList for int, internally jvm maps to ArrayList < Integer > type of objects. 

 Now the problem is ArrayList hold consecutive references to objects stored at disparate locations in jvm managed memory space. But my initial idea was to have consecutive storage of actual data objects AND NOT JUST REFERENCES ! But I don't know how to achieve it ?   

One solution I can think of is allocation of large size Byte Array thereby creating a consecutive memory buffer store ! And developer need to manage how to get some int data stored in this manually created buffer (lets us say if we model as 2 bytes/int) at 101 index location if there are 1000 int objects stored in 2000 preallocated bytes !

Same feature is also desirable with complex data types. Although it can be difficult while dealing with consecutive memory allocation for complex objects as one needs to calculate the structure size and preallocate Byte Buffer of (structureSize \* No. of Elements Expected)

I cannot think of any other efficient way or solution possible for consecutive allocation of objects (primitive/complex>. Can anyone help me ?

Answer: 
There is no single solution that meets your requirements.

If you want an array-like structure of a primitive type, then you need to use an array of that primitive type or some class that wraps an array of that primitive type1. If you try to implement a custom class, it cannot be generic (on the primitive type) because primitive types cannot be used as generic type parameters. Therefore, it cannot be an `ArrayList` or any other type derived from `Collection`.

If you want an array-like structure of a reference type then you can either use an array, or an `ArrayList`.

---

In short, if you are not willing to use the wrapper types (`Integer`, etc):

* You have to treat primitive and non-primitive types separately.
* It is best (from an API design and type-safety perspective) to treat the individual primitive types separately.

---

Hypothetically, you *could* create an array-like type that implemented an array of any primitive type, depending on a runtime parameter. Just define lots of methods / overloads for getting/setting different primitive types. However, that would not be compile-time type safe, and the API would be *ugly* ... to say the least.

Your idea of using a `ByteBuffer` is essentially the same as the above ... or worse, if you are anticipating that application code does all of indexing and encoding/decoding between bytes and the notional "array" basetype. (And you'd be better off starting from a `byte[]` rather than a `ByteBuffer`.)

---

1 - For example, one of the Trove collection classes.
There are a lot of primitive collections projects, here are some of them:

1. [org.apache.commons.collections.primitives](http://commons.apache.org/proper/commons-primitives/apidocs/org/apache/commons/collections/primitives/package-summary.html)
2. [High Performance Primitive Collections](http://labs.carrotsearch.com/hppc.html)
3. [Trove](http://trove.starlight-systems.com/)

But be aware, that using such things, you are getting tight to implementation. There is no common primitive collection API.