Dynamic Array in Java

Nov 22, 2013 at 3:58am

Dynamic Array in Java

Hi All,

Is it possible to have dynamic arrays in Java?

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

#272030
Nov 22, 2013 at 4:40pm
#273528
Nov 23, 2013 at 5:47am

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.

#273566
Nov 24, 2013 at 4:01am

`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.

#273633
Nov 24, 2013 at 8:27am

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.

#273659
Nov 25, 2013 at 2:40am

Thanks fellas, plenty to get stuck in to.

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

#273703
Nov 25, 2013 at 2:58am

These are general Java issues, not specifically Max ones.

Here’s a trail for the Collections machinery:

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

#273705

You must be logged in to reply to this topic.