Forums > Java

Dynamic Array in Java

November 22, 2013 | 3:58 am

Hi All,

Is it possible to have dynamic arrays in Java?

How can I replicate .push() , splice() functions?



November 23, 2013 | 5:47 am

Use ArrayList. It’s by far the fastest implementation of List for Java. (Vector is only for thread safe and legacy uses, and requires more overhead)

Sample usage:

List mylist = new ArrayList();

Most of the utility functions you’re looking for will be with the list or in the Collections utility class. (E.g. Collections.sort(mylist))

I highly, highly recommend spending a little time with the collections tutorial; it’ll make a worthwhile difference.


November 24, 2013 | 4:01 am

`ArrayList` is the fastest `List` implementation for access to individual elements (retrieval or update) – constant-time access – but the worst for resizing (order(N)). `LinkedList` is fastest for adding to or removing from either end, but order(N) performance for access.


November 24, 2013 | 8:27 am

To add to what Nick said, the capacity and size of a List are different things, so you can make the capacity larger than the size to reduce resizing. If you use ArrayList, you want to be aware of this because its default size is 10, IIRC!

Linked list is bad for random access, but does fine with iteration. (Comparable to ArrayList)

All of this depends on your use. I’d also read up on implementing Comparator and how that works with sorted sets/maps. The other key thing to understand is hashcode() and equals() and how they function in collections.


November 25, 2013 | 2:40 am

Thanks fellas, plenty to get stuck in to.

Is there any mention of this in Max’s java documentation?


November 25, 2013 | 2:58 am

These are general Java issues, not specifically Max ones.

Here’s a trail for the Collections machinery:

http://docs.oracle.com/javase/tutorial/collections/


Viewing 7 posts - 1 through 7 (of 7 total)