Basic ArrayList Operations and Wrapper Class Methods

This web page gives examples of how to call the ArrayList Operations properly for ArrayLists that contain Integer or Double objects.

This page also outlines what you need to know for the AP Exam regarding wrapping and unwrapping values with the Integer and Double classes.

The reason for using the wrapper classes is primarily because some data structures like an ArrayList can hold only objects not primitive types.

Therefore, if we wish to store int or double values in an ArrayList we must wrap them first as Integer or Double objects. However, thanks to what we call autoboxing, we don't have to stop and construct the Integer or Double objects ... its done automatically. We'll show you how to do that.

However, before we get going here are the ArrayList methods you need to be able to use for the AP Exam:

class java.util.ArrayList

int size ()
returns the number of elements currently in the list
boolean isEmpty ()
returns true if the list is empty or false otherwise
boolean add (Object obj)
appends the object to the end of the list
void add (int index, Object obj)
inserts obj before the element at index or after the last element if index equals the size of the list
Object get (int index)
returns the element at index
Object set (int index, Object obj)
replaces the element at index with obj and returns the old element
Object remove (int index)
removes and returns the element at index and then shifts any subsequent elements to the left
   
The following method is NOT
required for the AP Exam but is very helpful at times. Use it only if you are instructed to.
boolean remove (Object obj)
searches for obj in the list. If it finds obj then it is removed and any subsequent elements are shifted to the left and true is returned. If it doesn't find obj then it returns false.

Jump to Section on how to use each of these methods above

ArrayList Constructors.

Here is how to construct a templated ArrayList to be referred to by the variable list:

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

Examples of the templated ArrayList declarations are:

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

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

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

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

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


Section 1 ... The Integer Class.

class java.lang.Integer implements java.lang.Comparable

All example code on this web page assumes that ...

an ArrayList called list holding type Integer objects has been constructed using:     ArrayList < Integer > list = new ArrayList < Integer > ( );

or an ArrayList called list holding type Double objects has been constructed using:     ArrayList <Double> list = new ArrayList <Double> ( );

a Scanner variable reader has been declared with:    Scanner reader = new Scanner (System.in);

a Random variable gen has been declared with:    Random gen = new Random ();

 

1. To wrap an int as an Integer you must construct an Integer object. There are two ways to do it. First, get the int value from the keyboard or just assign an int variable a value as in ...

int value = 15;

then construct the Integer object ...

Integer intObj = new Integer(value);

You could have done this in one line with ...   Integer intObj = new Integer(15);

or you could do ...   Integer intObj = new Integer(reader.nextInt());

or you could do ...   Integer intObj = new Integer(gen.nextInt(1000));

then to add the Integer value to the list you would use ...

list.add(intObj);

this adds intObj to the end of the list, but if the list is empty then it adds it at index 0.

Alternately with an anonymous constructor you could have just written ...

list.add(new Integer(15));

However, starting with Java 5.0, you can rely on autoboxing and just do ...

list.add(15);

and 15 gets automatically wrapped as an Integer object and added to the ArrayList.

This means that you can also do ...

list.add(reader.nextInt());

and

list.add(gen.nextInt(1000));

 

2. To add the Integer to a specific location in the list at position index ...

list.add(index, intObj);

Note: to add a value to the front of a list just use ...

list.add(0, intObj);

where the first parameter is zero.

 


3. To unwrap an int value contained in the Integer variable intObj, call the intValue() method as in ...

int num = intObj.intValue();

 

4. If you need to get a value out of list at index formally, you need to first extract the Integer object using get, cast it to Integer and then call the intValue() method as in ...

int num = ( (Integer) list.get(index) ) . intValue();

however, beginning with Java 5.0 you don't need to cast or call intValue(), so the following would suffice ...

int num = list.get(index);

Java will cast the value for you and automatically unbox it.

 

5. You can check to see if two Integer objects contain the same value by using the equals() method without unwrapping them, because Integer class implements Comparable.

int value1 = 99;

int value2 = 99;

Integer intObj1 = new Integer(value1);

Integer intObj2 = new Integer(value2);

if ( intObj1.equals(intObj2) )

System.out.println("The values are equal.");

The System.out.println will be executed if the result is true and it is here.

 

6. You can print the value stored in an Integer object by calling the toString() method of the Integer class either explicitly or implicitly.

System.out.println(intObj1.toString());   // explicitly

System.out.println(intObj2);   // implicitly

 


7. You can also compare two Integer objects without unwrapping them. Remember that if zero is returned by the compareTo() method, the two objects are equal. If a negative value (most systems -1) is returned it means that the receiver object (this) is less than the parameter object. If a positive value (most systems +1) is returned it means that the receiver object (this) is greater than the parameter object.

Integer intObj1 = new Integer(99);

Integer intObj2 = new Integer(100);

if ( intObj1.compareTo(intObj2) == 0)

System.out.println("The values are equal.");

else if ( intObj1.compareTo(intObj2) > 0)

System.out.println("The value of intObj1 is greater than the value of intObj2.");

else // if ( intObj1.compareTo(intObj2) < 0)

System.out.println("The value of intObj1 is less than the value of intObj2.");


Section 2 ... The Double Class.

class java.lang.Double implements java.lang.Comparable

 

1. To wrap a double as a Double you must construct a Double object.

double value = 15.3;

Double floatObj = new Double(value);

then to add to the end of the list:

list.add(floatObj);

Alternately with an anonymous constructor ...

list.add(new Double(15.3));

or utilizing autoboxing just ...

list.add(15.3);

 

2. To add the Double to a specific location in the list at position index use ...

list.add(index, floatObj);

Again, to add a value to the front of a list just use ...

list.add(0, floatObj);

where the first parameter is zero.


3. To unwrap a double value contained in the Double variable floatObj use...

double num = floatObj.doubleValue();

 

4. If you need to get a value out of list at index formally, you need to first extract the Double object using get, cast it to Double and then call the doubleValue() method as in ...

double num = ( (Double) list.get(index) ) . doubleValue();

however, beginning with Java 5.0 you don't need to cast or call doubleValue(), so the following would suffice ...

double num = list.get(index);

Java will cast the value for you and automatically unbox it.

 

5. You can check to see if two Double objects contain the same value by using the equals() method without unwrapping them. This is possible because the Double class implements Comparable.

Double floatObj1 = new Double(99.9);

Double floatObj2 = new Double(99.9);

if ( floatObj1.equals(floatObj2) )

System.out.println("The values are equal.");

 

6. You can print the value stored in an Double object by calling the toString() method of its class.

System.out.println(floatObj1.toString());   // explicitly

System.out.println(floatObj2);   // implicitly

 


7. You can also compare two Double objects without unwrapping them. Remember that if zero is returned by the compareTo() method, the two objects are equal. If a negative value (most systems -1) is returned it means that the receiver object (this) is less than the parameter object. If a positive value (most systems +1) is returned it means that the receiver object (this) is greater than the parameter object.

Double floatObj1 = new Double(99.9);

Double floatObj2 = new Double(100.0);

if ( floatObj1.compareTo(floatObj2) == 0)

System.out.println("The values are equal.");

else if ( floatObj1.compareTo(floatObj2) > 0)

System.out.println("The value of floatObj1 is greater than the value of floatObj2.");

else // if ( floatObj1.compareTo(floatObj2) < 0)

System.out.println("The value of floatObj1 is less than the value of floatObj2.");


Section 3 ... Using the ArrayList operations.

For the demonstration code for each of the following ArrayList operations, we will assume the following code that constructs an ArrayList of Integer objects and adds values to it:

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

list.add(5);

list.add(10);

list.add(15);

 

1. int size() - returns the number of elements currently in the list (the logical size NOT the physical size)

list.size() returns the value 3

 

2. boolean isEmpty() - returns true if the list is empty or false otherwise

list.isEmpty() returns the value false

 

3. boolean add (Object obj) - appends the object to the end of the list

list.add(20) adds 20 to the end of the list so that list contains 5, 10, 15, 20.

 

4.void add (int index, Object obj) - inserts obj before the element at index or after the last element if index equals the size of the list

list.add(0, 3) shifts all values down and then adds 3 to the first of the list so that list contains 3, 5, 10, 15, 20.

 

list.add(2, 8) shifts all values beginning at index 2 down and then adds 8 at index 2 in the list so that list contains 3, 5, 8, 10, 15, 20.

 

list.add(6, 25) adds 25 to the end of the list so that list contains 3, 5, 8, 10, 15, 20, 25.

 

5. Object get (int index- returns the element at index

list.get(0) returns 3.

list.get(1) returns 5.

list.get(2) returns 8.

list.get(3) returns 10.

list.get(4) returns 15.

list.get(5) returns 20.

list.get(6) returns 25.

 

6. Object set (int index, Object obj) - replaces the element at index with obj and returns the old element

list.set(0, 2) replaces the value 3 at postion 0 with the value 2 so that the list contains 2, 5, 8, 10, 15, 20, 25.

list.set(3, 12) replaces the value 10 at postion 3 with the value 12 so that the list contains 2, 5, 8, 12, 15, 20, 25.

list.set(6, 23) replaces the value 25 at postion 6 with the value 23 so that the list contains 2, 5, 8, 10, 15, 20, 23.

list.set(7, 30) throws a java.lang.IndexOutOfBoundsException: Index: 7, Size: 7 error.

Note: You cannot use set on an empty list!!! it throws a java.lang.IndexOutOfBoundsException: Index: 0, Size: 0

 

7. Object remove (int index) - removes and returns the element at index

list.remove(4) removes the value 15 at postion 4 and shifts the values down so that the list contains 2, 5, 8, 10, 20, 23.

list.remove(0) removes the value 2 at postion 0 and shifts the values down so that the list contains 5, 8, 10, 20, 23.

list.remove(4) removes the value 23 at postion 4 and does no shifting so that the list contains 5, 8, 10, 20.

list.remove(4) throws a java.lang.IndexOutOfBoundsException: Index: 4, Size: 4

 

8. Object remove (Object obj) - removes and returns the object obj if it exists in the list

the current list contains 5, 8, 10, 20.

list.remove(new Integer(8)) removes the Integer object value 8 from the list and shifts down 10 and 20 so that the list contains 5, 10, 20.

list.remove(new Integer(15)) does nothing and the list still contains 5, 10, 20.

 


Back to Top