Wrapper classes
- Defined, wrapper
classes are Java classes that were created to hold one primitive data
value. Examples are Integer,
Double,
Byte, Short. Objects of these types hold one value of their
corresponding primitive type (int, double, byte, short). They are
used when you desire to store primitive data types in Java structures
that require objects (e.g. JLists, ArrayLists, JComboboxes, JTables).
- Java 1.5 introduced a concept called "autoboxing" and
"autounboxing". Before this came along you had to call methods of
the wrapper classes in order to get and set their values, now you
can use them anywhere the primitive data types are used, Java
automatically pulls the primitive values in/out of the objects. e.g.
Double x = 1.0; // boxes 1.0 into
a Double object
double y = x + 22; //
here the value of x is automatically unboxed to be used in the
expression
- Wrapper classes provide the parse methods (parseDouble, parseInt)
and useful constants such as MAX_VALUE, MIN_VALUE
- Wrapper classes aren't as efficient as primitive types, but they
are
handy.
ArrayList
- The ArrayList class implements a growable array of
objects. ArrayLists cannot hold primitive data types such as
int, double, char, and long (they can hold String since String is an
object, and wrapper class objects (Double, Integer).
- Like an array, it contains components that can be accessed using
an integer index.
- However, the size of a
ArrayList
can grow or shrink
as
needed to accommodate adding and removing items after the ArrayList
has been created.
- To use a ArrayList object you must import java.util.*;
- To create a ArrayList:
ArrayList myList = new ArrayList(100); //
creates a
new ArrayList with an initial capacity of 100
- To add items to a ArrayList, use one of two variations of the
add()
method
myList.add("Fred Flintstone"); // add Fred
to the
end of the list
myList.add(2, "Barney Rubble"); // inserts
Barney
rubble into position 2 (counting from 0) in the list, moves existing
items.
- To replace an item at a given position use the set method
myList.set(2,"Roger
Ramjet"); // replaces the item at position 2 with "Roger
Ramjet"
- to remove items from a ArrayList, use the remove() and/or clear()
method.
myList.remove(4); // removes item in position 4 (5th item
counting from 0)
myList.remove("Barney Rubble"); // removes item Barney Rubble
from the list
myList.clear(); // removes all the items
- to determine how many elements are in the ArrayList use the size
method
int count = myList.size(); // count now holds the number of
items
added to the ArrayList
- to retrieve a particular element use the get() method.
Note, since ArrayLists are designed to hold any type of object,
the
get method returns a value of the generic Object type. You must cast
this result to the data
type that was used when loading the array.
String str = (String)myList.get(3); // get the item
at
position 3 (fourth item counting from 0)
String first = (String)myList.get(0); // get the first item, ie item at
position 0.
- to process all items in a ArrayList, use a for loop. E.g.
for(int i = 0; i < myList.size(); i++)
{
String str = (String)myList.get(i);
System.out.println(str);
}
Using generics with ArrayLists
- You can declare and create a ArrayList and specify the type of
object it is to hold (instead of allowing it to hold any type of
object).
- If you do this, then you don't have to cast the data when
accessing the item with a get, also, it makes the code better as you've
explicity stated what your ArrayList is to hold
- To do this, use <data type> after the ArrayList in
your
declaration line.
- The data type can be any object type, e.g. String, Double,
Integer.
- e.g.
ArrayList<String> myList
= new ArrayList<String>(100);
// create a ArrayList that will hold strings, initially size it to 100
- Adding is the same as before
myList.add("Fred Flintstone);
- To access an item, it isn't necessary to type cast
the
get statement, eg.
String name = myList.get(0);
//
get the first item
- Removing is the same as before
myList.remove(0); // remove the first item
myList.clear(); // clean out the list