<p>You probably want an <a href="https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html" rel="nofollow noreferrer">ArrayList</a>. They can be resized, however. Python has a similar thing.</p>
<p>You should become familiar with the oracle docs of which the above link is a part. They are quite informative.</p>
<p>An older type is the Vector, but it has been largely replaced by ArrayList.</p>

CC BY-SA 4.0
2023-11-20T22:00:37.387Z
Thank you.  I suspected that `ArrayList` would be the choice, after all if implementing a "better" array then removing the fixed length condition would be an obvious one to do.  Thanks also for the pointer to the oracle docs - I hadn't figured out the "oracle" of java docs as yet via random internet searches.
Andrew Stacey
CC BY-SA 4.0
2023-11-20T22:17:19.913Z
When Sun (inventer of Java) left the scene, Oracle bought out the company. Many employees left for Oracle also.
Buffy
CC BY-SA 4.0
2023-11-20T00:07:25.553Z
2023-11-20T00:07:25.553Z
Buffy
<p><strong>TL;DR</strong> I'm looking for a replacement for the primitive array data type in Java that works better with generic data types.</p>
<p><em>Background</em>: I'm teaching A level CS in the UK (age 16-18) and we're using Java as our primary language. The students are already familiar with python.</p>
<p>To teach some data types, we're coding them in Java. The specification regards a fixed-length, single type array as a primitive data type so to code, say, a queue we build a class with an array to hold the data in the queue and then add methods accordingly.  The array data type in Java matches that of the specification nicely, so this works quite well.</p>
<p>Partly for my own interest (I have experience of other languages, but haven't done much Java before) but also because I like to have an example for the students of &quot;what if you wanted to do a bit more&quot;, then I wanted to extend my queue class to have a generic type.  But this seems quite complicated since the primitive array type doesn't work well with generic types (this is well explored over on StackOverflow, see <a href="https://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java">https://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java</a> for example) - unlike in other similar languages, such as C#.</p>
<p>So my question is as to whether there is a replacement for the array data type in Java which is sufficiently similar to array that it doesn't change the learning experience (so, single data type and fixed length) but which can take a generic type.</p>

<java><arrays><data-types>
Is there an array-like data type in Java that can cope with generic types better than the built-in array?
# Is there an array-like data type in Java that can cope with generic types better than the built-in array?

(Asked by: username_0 on 2023-11-19)

**TL;DR** I'm looking for a replacement for the primitive array data type in Java that works better with generic data types.

*Background*: I'm teaching A level CS in the UK (age 16-18) and we're using Java as our primary language. The students are already familiar with python.

To teach some data types, we're coding them in Java. The specification regards a fixed-length, single type array as a primitive data type so to code, say, a queue we build a class with an array to hold the data in the queue and then add methods accordingly. The array data type in Java matches that of the specification nicely, so this works quite well.

Partly for my own interest (I have experience of other languages, but haven't done much Java before) but also because I like to have an example for the students of "what if you wanted to do a bit more", then I wanted to extend my queue class to have a generic type. But this seems quite complicated since the primitive array type doesn't work well with generic types (this is well explored over on StackOverflow, see <https://stackoverflow.com/questions/529085/how-to-create-a-generic-array-in-java> for example) - unlike in other similar languages, such as C#.

So my question is as to whether there is a replacement for the array data type in Java which is sufficiently similar to array that it doesn't change the learning experience (so, single data type and fixed length) but which can take a generic type.

**Tags:** java, arrays, data-types


---

## 1 answers

### Answer by username_1 on 2023-11-20. Score: 2 | Accepted answer

You probably want an [ArrayList](https://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html). They can be resized, however. Python has a similar thing.

You should become familiar with the oracle docs of which the above link is a part. They are quite informative.

An older type is the Vector, but it has been largely replaced by ArrayList.

> **Comments:**
> **username_0**: Thank you.  I suspected that `ArrayList` would be the choice, after all if implementing a "better" array then removing the fixed length condition would be an obvious one to do.  Thanks also for the pointer to the oracle docs - I hadn't figured out the "oracle" of java docs as yet via random internet searches.  
> **username_1**: When Sun (inventer of Java) left the scene, Oracle bought out the company. Many employees left for Oracle also.  

---

