JAVATM- The Beginnings

ArrayLists

Suppose you create a Java array of two integers.

        int[] myIntegers = { 1, 2 };

You decide later that you want to add another value to the array. You now have to create a new array with three elements, copy the previous elements plus the new value to it, then assign the result to the myIntegers reference variable. If an ArrayList were used instead, an new element could be added without explicitly copying the previous contents.

Section 1 - ArrayList Definition

An ArrayList is an object which models the behavior of a Java array. It is one of the container classes in the Java API. Container classes allow you to place one or more object references inside another object.

The main advantage of ArrayLists over Java arrays is that you can add an element to an ArrayList without explicitly copying the previous contents.

Section 2 - ArrayList Declarations

The general form of an ArrayList declaration is as follows:

        ArrayList<referenceType> listName = new ArrayList<>();

An ArrayList is a container of references objects of a particular type. The type of the object reference is generic, and is specified in triangular brackets between the word ArrayList and the name of the list. A set of empty triangular brackets is placed in the constructor call between the word ArrayList and the parentheses. In generic notation, the symbol e is normally used for referenceType.

Section 3 - ArrayLists Contain Object References

Copyright ©2017 by Ralph Lecessi Incorporated. All rights reserved.



Back to ralphlecessi.com