Java ArrayList

Java ArrayList Print E-mail
Contributed by Joe   
Sunday, 09 July 2006

Java ArrayList Overview

ArrayList is a resizable-array which implements all optional list operations, and permits all elements, including null.

An ArrayList has a capacity, which is the number of elements in the internal array that contains the elements of the list. When the number of elements exceeds the capacity, a new array, with a larger capacity, must be created. In addition to the List and Collection methods, ArrayList defines a couple of methods that help you manage this capacity. .Java ArrayList is preferred in Java 1.2 and later,ArrayList use the new generics feature in Java 1.5.

 

Java ArrayList features

  • An array list automatically resizes itself whenever necessary.
  • An array list lets you insert elements into the middle of the collection.
  • An array list lets you delete items.
  • The ArrayList class actually uses an array internally to store the data you add to the array list.

Java ArrayList constructors lists.

 

Constructor

Summary

ArrayList()

Constructs an empty list with an initial capacity of ten.

ArrayList(Collection c)

 Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

ArrayList(int initialCapacity)

Constructs an empty list with the specified initial capacity.

Example: Creating a java ArrayList

To create an array list, you first declare an ArrayList variable, and then
call the ArrayList constructor to instantiate an array list object and assign
it to the variable. for example

ArrayList list = new ArrayList();

Here are a few things to note about creating array lists:

Your program must import either java.util.ArrayList or java.util.*.

Unlike an array, you don’t have to specify a capacity for an array list.
But you can if you want,like below eample

ArrayList signs = new ArrayList(100);

If you don’t specify a capacity for the array list, the initial capacity is set
to 10.

The capacity of an array list is not a fixed limit. The ArrayList class
automatically increases the list’s capacity whenever necessary.

To Java 1.5, you can also specify the type of elements the
array list is allowed to contain. For example, this statement creates an
array list that holds String objects:

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

Adding java ArrayList Elements

Here’s code that adds strings to an array list:
list.add(“IBM”);
list.add(“SUN”);
list.add(“MS”);


Hers' code to insert an object at a specific position

ArrayList<String> nums = new ArrayList<String>();
nums.add(“One”);
nums.add(“Two”);
nums.add(“Three”);
nums.add(“Four”);
nums.add(2, “Two and Three”);


After these statements execute, the nums array list contains the following
strings:
One
Two
Two and Three
Three
Four

Accessing Elements

To access a specific element in an array list, you can use the get method. It
specifies the index value of the element you want to retrieve. For example,
here’s a for loop that prints all the strings in an array list:

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

Nost people like to use an iterator to access all the elements .

ArrayList<String> nums = new ArrayList<String>();
nums.add(“One”);
nums.add(“Two”);
nums.add(“Three”);
nums.add(“Four”);
String s;
Iterator e = nums.iterator();
while (e.hasNext())
{
s = (String)e.next();
System.out.println(s);
}

Updating Elements

You can use the set method to replace an existing object with another
object. For example:

ArrayList<String> list = new ArrayList<String>();
list.clear();
list.add(“One”);
list.add(“Two”);
list.add(“Three”);

list.set(0, “Uno”);
list.set(1, “Dos”);
list.set(2, “Tres”);

Deleting Elements

To remove all the elements, use the clear method, like this:

emps.clear();

To remove a specific element, use the remove method. like this:

emps.remove(0);

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

Java ArrayList Questions

Given:
11. ArraryList a = new ArrayList();
12. a.add(“Alpha”);
13. a.add(“Bravo”):
14. a.add(“Charlie”);
15. a.add(“Delta”);
16.Iterator iter = a.iterator();
17.

Which two, added at line 17, print the names in the ArrayList in alphabetical order?
(Choose two)

A.  for (int i=0; i< a.size(); i++) System.out.println(a.get(i)));
B.  for (int i=0; i< a.size(); i++) System.out.println(a[i]);
C.  while( iter.hasNext() ) System.out.println(iter.next()) ;
D.  for (int i=0, i< a.size(); i++) System.out.println(iter[i]);
E.   for (int i=0; i< a.size(); i++) System.out.println(iter.get(i));


Related Articles:

Java HashMap tutorial 

Last Updated ( Wednesday, 23 August 2006 )

  home              contact us

 

©2006-2010 DeveloperZone.biz   All rights reserved     powered by Mambo Designed by Siteground