In Java, Arrays are used to store multiple items of the same time in an easy to
use structure. When you want to store multiple pieces of data and have data
types relate to each other (such as Employee name, address, hours that you can
use a set of parallel arrays or create an Employee class and use a single array
that holds the objects.
The problem with using Arrays is that you must set the size of the array when
it is created. For example:
Expand|Select|Wrap|Line Numbers
- Employee[] emparray = new Employee[10];
store more than 10 elements in this array, Java throws an error exception.
Having to initially set the size of such arrays is very limiting since you may
not know that you will have only at most (or exactly) 10 Employees.
One way to solve this problem is to use some of the built-in Data Structure
classes found in Java. The main purpose of such data structures is to make a
place you can store things like Employee objects that is not constrained by
size and that makes it easy to retrieve data from the data structure.
Java provides a data structure called an ArrayList. This works like a
combination of Linked List and Array in that you can store as much as you like
in this structure and it will grow accordingly. You can also access elements in
this structure by using an index value, much as you would with an Array.
There are some restrictions using ArrayLists is that they can only handle Class
objects and cannot easily handle basic data types such as integers and doubles.
If you want to store such information in an ArrayList you will have to first
convert it to object data types.
Java 1.4 and before wanted you to wrap ints into Integers, doubles into Doubles
etc.etc. This is what a fragment of Java 1.4 looked like:
Expand|Select|Wrap|Line Numbers
- List array= new ArrayList();
- for (int i= 0; i < 10; i++) {
- array.add(new Integer(i));
- int j= ((Integer)array.get(i)).intValue(); // get value back
Expand|Select|Wrap|Line Numbers
- List<Integer> array= new ArrayList<Integer>();
- for (int i= 0; i < 10; i++) {
- arrray.add(i);
- int j= array.get(i); // get value back
add some special methods to your Classes. These methods are the equals(),
hashCode() and compareTo() methods. This will be explained later on..
Creating an ArrayList is done by importing the java.util.*; package or just the
single class itself: import java.util.ArrayList class and then creating an
instance of the ArrayList class:
Expand|Select|Wrap|Line Numbers
- import java.util.*;
- ...
- List<PersonClass> arlist = new ArrayList<PersonClass>();
is to use the name of the class that will be used to create the objects that
will be stored in the list. This class name is placed between a pair of < >
(<Employee> ) symbols and placed after the ArrayList class name both places it
is used when creating the arraylist object. In this case, the class that will
be used to create objects to be stored in this arraylist is the Employee class.
Using the Arraylist
Once an ArrayList is created, you can create the objects that will be added and
then add them to the list. This is done with the add() command:
Expand|Select|Wrap|Line Numbers
- PersonClass temp = new PersonClass ("NW123");
- arlist.add(temp);
it to a particular index position in the list you would use the command:
Expand|Select|Wrap|Line Numbers
- PersonClass temp = new PersonClass ("NW123");
- arlist.add(3, temp);
If you wanted to replace the element at position 3 with the new object just
created, you would use:
Expand|Select|Wrap|Line Numbers
- PersonClass temp = new PersonClass ("E123");
- arlist.set(3, temp);
Expand|Select|Wrap|Line Numbers
- arlist.remove(5);
Expand|Select|Wrap|Line Numbers
- Employee x;
- x = arlist.get(5);
holding) you would use:
Expand|Select|Wrap|Line Numbers
- int empsize = arlist.size();
Expand|Select|Wrap|Line Numbers
- System.out.println("The size is "+empsize () );
Expand|Select|Wrap|Line Numbers
- for(PersonClass e : arlist ) {
- System.out.println("The name is "+e.getLname() );
- }
as an Iterator would do. Iterators iterate over a List starting at the first
element, ending at the last element; so the obove loop visits each element in
the same order as the loop below would do:
Expand|Select|Wrap|Line Numbers
- for( int i=0; i<arlist.size(); i++) {
- PersonClass e = arlist.get( i );
- System.out.println("The info is "+e.getEmpID(), +e.getLname() );
- }
The i variable is used as the ArrayList index and used with the get() method
to return the object. The PersonClass object e is only used to hold the
reference to the object in the list.
This completes this short introduction of the ArrayList class. There's another
List implementation: the LinkedList that behaves similar to the ArrayList.
Both type of lists have their pros and cons: an ArrayList is extremely fast
when you want to access elements given an index value. A LinkedList is very
fast when it comes to deletion or insertion of elements anywhere in the list.
Given the characteristics of your algorithm select the correct type of list.
Note that your algorithm itself should refer to the particular list as an object
of type 'List', i.e. just the interface type. This makes it very easy to select
another type of List when needed; just the construction part of the List would
know about what type of List will be actually constructed.
There are more datastructures available in the Collections framework; they are
the subject of other articles.