Dynamic Array in Java

    Nov 22 2013 | 11:58 am
    Hi All,
    Is it possible to have dynamic arrays in Java?
    How can I replicate .push() , splice() functions?

    • Nov 23 2013 | 1:47 pm
      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.
    • Nov 24 2013 | 12:01 pm
      `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.
    • Nov 24 2013 | 4:27 pm
      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.
    • Nov 25 2013 | 10:40 am
      Thanks fellas, plenty to get stuck in to.
      Is there any mention of this in Max's java documentation?
    • Nov 25 2013 | 10:58 am
      These are general Java issues, not specifically Max ones.
      Here's a trail for the Collections machinery: