Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

Java ArrayList 1.7

Name: Anonymous 2011-11-30 10:39

http://www.docjar.com/html/api/java/util/ArrayList.java.html


  374       /**
  375        * Returns the element at the specified position in this list.
  376        *
  377        * @param  index index of the element to return
  378        * @return the element at the specified position in this list
  379        * @throws IndexOutOfBoundsException {@inheritDoc}
  380        */
  381       public E get(int index) {
  382           rangeCheck(index);
  383  
  384           return elementData(index);
  385       }

 596       /**
  597        * Checks if the given index is in range.  If not, throws an appropriate
  598        * runtime exception.  This method does *not* check if the index is
  599        * negative: It is always used immediately prior to an array access,
  600        * which throws an ArrayIndexOutOfBoundsException if index is negative.
  601        */
  602       private void rangeCheck(int index) {
  603           if (index >= size)
  604               throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
  605       }



What's the point of having a range check if it only checks if the index is >= to the size and not if it's < 0. This seems stupid and a waste of a method call.



  171       /**
  172        * Increases the capacity of this <tt>ArrayList</tt> instance, if
  173        * necessary, to ensure that it can hold at least the number of elements
  174        * specified by the minimum capacity argument.
  175        *
  176        * @param   minCapacity   the desired minimum capacity
  177        */
  178       public void ensureCapacity(int minCapacity) {
  179           if (minCapacity > 0)
  180               ensureCapacityInternal(minCapacity);
  181       }
  182  
  183       private void ensureCapacityInternal(int minCapacity) {
  184           modCount++;
  185           // overflow-conscious code
  186           if (minCapacity - elementData.length > 0)
  187               grow(minCapacity);
  188       }


ensureCapacity is never called, yet they have it in the class.


BLOAT

Name: Cudder !MhMRSATORI!FBeUS42x4uM+kgp 2011-12-01 3:21

If the compiler/interpreter cannot even optimize such a call, how can it be possible, as some claim, for Java to be as fast as C or C++?
Very aggressive space/speed tradeoff (.NET is similar) ---  things like GC make memory allocation almost intantaneous (until you run out of memory and GC is needed, that is), and inlining everything also has similar effects.

(This only works up to the size of the cache, and then cache misses start occurring and everything slows down again.)

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List