Header javaperspective.com
JavaPerspective.com  >   Intermediate Tutorials  >   3. Collections  >   3.1. The class ArrayList

3.1. The class ArrayList
Last updated: 31 January 2013.

This tutorial gives out tips about using the class ArrayList.

The class ArrayList implements the interface List as an unsynchronized, resizable and ordered array of elements. The size of an ArrayList instance grows when elements are added to it. The following sample shows how to create an ArrayList of strings with the default constructor:

List<String> myArrayList = new ArrayList<String>();

To add elements to the ArrayList instance, you can use the method Add(E e) which appends the element e to the end of the ArrayList instance:

myArrayList.add("Africa");
myArrayList.add
("America");
myArrayList.add
("Antarctica");
myArrayList.add
("Asia");
myArrayList.add
("Europe");

The method get(int index) returns the element at the specified index. You can iterate over the ArrayList instance with a for statement like this:

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

You can also use a for-each statement as shown below:

for(String element : myArrayList)
   
System.out.println(element);

Of course, you can remove one or more elements by calling the method remove(int index), remove(Object o), removeRange(int fromIndex, int toIndex) or clear. For more information about the methods of the class ArrayList, please refer to the Java API.


You are here :  JavaPerspective.com  >   Intermediate Tutorials  >   3. Collections  >   3.1. The class ArrayList
Next tutorial :  JavaPerspective.com  >   Intermediate Tutorials  >   3. Collections  >   3.2. The class HashSet

Copyright © 2013. JavaPerspective.com. All rights reserved.  ( Terms | Contact | About ) 
Java is a trademark of Oracle Corporation
Image 1 Image 2 Image 3 Image 4 Image 5 Image 6 Image 7