Java Concepts‎ > ‎

Arrays and ArrayLists

An array is an object that can store multiple values of the same data type (numbers, characters, strings, or objects).
An array can be created (constructed) using the following statement:

int[] numbers = new int[10];

Note: The call to an array's constructor (new int[10]) instructs the constructor to create a new array containing 10 elements and assign its memory address to the numbers variable that has int[] data type. The first part of the statement alone (int[] numbers;) will not create an array, but only declare a variable

After an array is created, its size cannot be changed. Each element in the array can be accessed by its subscript (used as the array's internal index):

numbers[0] = 101;   // integer 101 is assigned to the first element of the array
numbers[9] = 110;   // integer 110 is assigned to the last element of the array

Note: Subscript 9 is the last subscript in the array. When we called the constructor (new int[10]), we used the array's size declarator, which is greater than its highest subscript.

We cannot copy an array using an usual assignment statement, because the array variables hold memory locations, not actual arrays:

int[] figures = numbers;   // this will copy the array's memory location to a new array variable

To copy an array we should use the clone() method:

int[] figures = (int[]) numbers.clone();

Note: We need to use the data type casting in this case, because the clone() method's return type is an Object.

To copy elements from one array to another we can use the System.arraycopy method, for example:

int[] nums = new int[30];
System.arraycopy(numbers, 0, nums, 20, 10);

Note: This statement will copy 10 elements of the numbers array and place them into the nums array's elements 20 through 29.


An ArrayList
is a class in Java API and allows storage of objects. The ArrayList class supplies its own methods for inserting and removing elements and automatically adjusts its size when items are added or removed.. An ArrayList object can be created using the following statement:

ArrayList<DataType> objectList = new ArrayList<DataType>();
ArrayList<Property> allProperties = new ArrayList<Property>();

Note: The call to the array's constructor (new ArrayList<Property>()) instructs the constructor to create a new ArrayList object and assign its memory address to the allProperties variable that has a data type of ArrayList<Property>.
Note: To store objects of the data type Object we can use an untyped ArrayList:    
ArrayList allObjects = new ArrayList().

To illustrate, let us use the Commercial class (a subclass of Property superclass) from a Real Estate Business program to construct several Commercial objects:
Commercial cp01 = new Commercial
    ("500 Port St., Omegaville, NA", 1500000.00, 4500, 2, "Multi-Family");
Commercial cp02 = new Commercial
    ("600 Commercial Blvd., Alphaville, NA", 3000000.00, 9000, 4, "Hotel");
Commercial cp03 = new Commercial
    ("700 Mill Rd., Verona, NA", 899000.00, 2600, 2, "Office");
Commercial cp04 = new Commercial
    ("800 Plant St., Industrial City, NA", 1290000.00, 3800, 4, "Business");

These Commercial objects can be added to the ArrayList allProperties using the add() method:
allProperties.add(cp01);
allProperties.add(cp02);
allProperties.add(cp03);
allProperties.add(cp04);

Each element of the allProperties can be retrieved using the get() method and can be set to a new value using the set() method. Similar to arrays, an ArrayList reports the number of its elements using a method named size(), which can be used to access the elements using a loop:

for (int i = 0; i < allProperties.size(); i++) {
            System.out.println(allProperties.get(i));

Note: To report their length, strings have a method length() and arrays have a public field named length.

ċ
DryCleaners.zip
(17k)
Linas Vaicekavicius,
Oct 12, 2010, 4:24 AM
Comments