For this reason java provides a standard array class

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Java provides a standard array class named ArrayList. ArrayList-based arrays are less efficient than the traditional Java arrays, but in some programs they are very useful. The most important feature of ArrayList-based arrays is that they can grow dynamically, i.e., the memory space of an array is increased automatically if necessary. ArrayList arrays can store only (references to) objects. When you create an ArrayList array, you should specify what kinds of objects you intend to store with the array. An ArrayList-based array can be created with a statement like ArrayList<Integer> array_of_integers = new ArrayList<Integer>() ; By writing <Integer> after the class name you can specify that the array will store objects of type Integer. There is no need to specify a length or a capacity for the array because the array can grow automatically when necessary. ArrayList is a so-called generic class whose characteristics can be fine-tuned at the moment when an object of the class is created. By writing a class name inside angle brackets, < >, after the name ArrayList, we can stipulate what kinds of objects the array will store. The class name inside the angle brackets is a type parameter for the generic class. In general, an ArrayList array like ArrayList<SomeClass> some_array = new ArrayList<SomeClass>() ; can store objects of SomeClass or objects of some subclass of SomeClass. The simplest way to add elements to an ArrayList-based array is to use the method add() which can add a new element to the end of an array. For example, the statements array_of_integers.add( 123 ) ; array_of_integers.add( 456 ) ; array_of_integers.add( 789 ) ; would add three elements of type int to the array of type ArrayList<Integer> that is created above. When these statements are executed right after the creation of the array, the number of array elements becomes 3. The integer values that are added to the array by the above statements are values, not objects. Therefore, automatic boxing operations happen when the above statements are executed. In a boxing operation, a value type is automati- 15.1 ArrayList class 503 cally converted to an object. The above int values are converted to Integer objects. As a result of the execution of the above statements, the first three positions in the array reference "boxes" that contain the integer values. An ArrayList array has a specific capacity in regard to the amount of elements it can hold. This capacity is consumed gradually when new array elements are added to the array. When the capacity to add new elements has been exhausted, it is enlarged automatically. The capacity of an array is enlarged so that a new and larger memory area is allocated for the array, the old array elements are copied to the new memory area, and the new memory area becomes the official internal memory area of the array. Traditional arrays have the length field which can be read when we want to know how many e...
View Full Document

This document was uploaded on 01/20/2014.

Ask a homework question - tutors are online