ArrayList Class

Package: java.util

The ArrayList package lets you create and maintain a special type of collection object: an array list. An array list is similar to an array but averts many of the most common problems of working with arrays, specifically the following:

check.png An array list automatically resizes itself whenever necessary.

check.png An array list lets you insert elements into the middle of the collection.

check.png An array list lets you delete items.

Constructors

Constructor

Explanation

ArrayList()

Creates an array list with an initial capacity of ten elements.

ArrayList(int capacity)

Creates an array list with the specified initial capacity.

ArrayList(Collection c)

Creates an array list and copies all the elements from the specified collection into the new array list.

Methods

Method

Explanation

add(Object element)

Adds the specified object to the array list. If you specified a type when you created the array list, the object must be of the correct type.

add(int index, Object element)

Adds the specified object to the array list at the specified index position. If you specified a type when you created the array list, the object must be of the correct type.

addAll(Collection c)

Adds all the elements of the specified collection to this array list.

addAll(int index, Collection c)

Adds all the elements of the specified collection to this array list at the specified index position.

clear()

Deletes all elements from the array list.

clone()

Returns a shallow copy of the array list. The elements contained in the copy are the same object instances as the elements in the original.

contains(Object elem)

Returns a Boolean value that indicates whether the specified object is in the array list.

containsAll (Collection c)

Returns a Boolean value that indicates whether this array list contains all the objects that are in the specified collection.

ensureCapacity (int minCapacity)

Increases the array list’s capacity to the specified value. (If the capacity is already greater than the specified value, this method does nothing.)

get(int index)

Returns the object at the specified position in the list.

indexOf(Object elem)

Returns the index position of the first occurrence of the specified object in the array list. If the object isn’t in the list, it returns -1.

isEmpty()

Returns a Boolean value that indicates whether the array list is empty.

iterator()

Returns an iterator for the array list.

lastIndexOf(Object elem)

Returns the index position of the last occurrence of the specified object in the array list. If the object isn’t in the list, it returns -1.

remove(int index)

Removes the object at the specified index and returns the element that was removed.

remove(Object elem)

Removes an object from the list. Note that more than one element refers to the object; this method removes only one of them. It returns a Boolean value that indicates whether the object was in the list.

remove(int fromIndex, int toIndex)

Removes all objects whose index values are between the values specified. Note that the elements at the fromIndex and toIndex positions are not themselves removed.

removeAll(Collection c)

Removes all the objects in the specified collection from this array list.

retainAll(Collection c)

Removes all the objects that are not in the specified collection from this array list.

set(int index, Object elem)

Sets the specified element to the specified object. The element that was previously at that position is returned as the method’s return value.

size()

Returns the number of elements in the list.

toArray()

Returns the elements of the array list as an array of objects (Object[]).

toArray(type[] array)

Returns the elements of the array list as an array whose type is the same as the array passed via the parameter.

Creating an array list

To create an array list, you declare an ArrayList variable and call the ArrayList constructor to instantiate an ArrayList object and assign it to the variable:

ArrayList friends = new ArrayList();

You can optionally specific a capacity in the ArrayList constructor:

ArrayList friends = new ArrayList(100);

Note that the capacity is not a fixed limit. The ArrayList class automatically increases the list’s capacity whenever necessary.

You can use the generics feature to specify the type of elements the array list is allowed to contain:

ArrayList<String> friends = new ArrayList<String>();

For more information, see Generics in Part 2.

Adding elements

You use the add method to add objects to the array list:

friends.add(“Bob Mitchell”);

If you specified a type when you created the array list, the objects you add via the add method must be of the correct type.

You can insert an object at a specific position in the list by listing the position in the add method:

ArrayList<String> nums = new ArrayList<String>();

nums.add(“One”);

nums.add(“Two”);

nums.add(“Three”);

nums.add(“Four”);

nums.add(2, “Two and a half”);

After these statements execute, the nums array list contains the following strings:

One

Two

Two and a half

Three

Four

Warning.eps If you use the add method to insert an element at a specific index position and there is not already an object at that position, the add method throws the unchecked exception IndexOutOfBoundsException.

Accessing elements

To access a specific element in an array list, use the get method and specify the index value (beginning with zero) of the element that you want to retrieve:

for (int i = 0; i < nums.size(); i++)

System.out.println(nums.get(i));

Here, the size method is used to set the limit of the for loop’s index variable.

You can also use an enhanced for statement, which lets you retrieve the elements without bothering with indexes or the get method:

for (String s : nums)

System.out.println(s);

Here, each String element in the nums array list is printed to the console.

To determine the index number of a particular object in an array list when you have a reference to the object, use the indexOf method:

for (String s : nums)

{

int i = nums.indexOf(s);

System.out.println(Item “ + i + “: “ + s);

}

Here, an enhanced for loop prints the index number of each string along with the string.

Updating elements

Use the set method to replace an existing object with another object within an array list. For example:

ArrayList<String> nums = new ArrayList<String>();

nums.add(“One”);

nums.set(0, “Uno”);

Here, an array list is created with a single string whose value is One. Then, the value of the first element is replaced with the value Uno.

Deleting elements

To remove all the elements, use the clear method:

emps.clear();

To remove a specific element based on the index number, use the remove method:

emps.remove(0);

Here, the first element in the array list is removed.

If you don’t know the index of the object you want to remove, but you have a reference to the actual object, you can pass the object to the remove method:

employees.remove(employee);

The removeRange method removes more than one element from an array list based on the starting and ending index numbers. This method removes all elements between the elements you specify, but not the elements you specify. Thus, remove Range(5, 8), for example, removes elements 6 and 7, but elements 5 and 8 aren’t removed.

tip.eps You can also use the removeAll method to remove all the objects in one collection from another collection. A similar method, retainAll, removes all the objects that are not in another collection.

Note that the clear method and the various remove methods don’t actually delete objects; they simply remove the references to the objects from the array list. Like any other objects, the objects in a collection are deleted automatically by Java’s garbage collector after the objects are no longer being referenced by the program.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset