Lists

Lists are dynamic structures that contain objects. Lists are similar to arrays, but they are dynamically sized to expand and shrink as you add and remove elements. There are a number of different implementations of lists that fulfill different needs (e.g. LinkedList, Stack) but the most commonly used list is the ArrayList class. The class contains a number of methods to add and remove objects, test if an object is in the list, get a count of the number of objects in the list, etc.

list = new ArrayList();
list.add("A");
list.add("B");

if (list.contains("B"))
   print("B is in the list");
[Important]Boxing primitives as Objects

Note that the Java collection classes store objects, and recall that the Java and BeanShell data types include both objects and primitives. What if we want to have a list of primitive int values which are primitives, not objects? Well it turns out that the Java class system has a 'box' object for each primitive data type. The box objects are used to contain the primitive values. In addition these classes provide methods that perform comparisons, convert the primitive to other data types, etc. The following table list the box class names that correspond to each primitive:

Table 3. Boxed class names

PrimitiveBoxed object
booleanBoolean
charChar
byteByte
shortShort
intInteger
longLong
floatFloat
doubleDouble

The boxed objects are described in the fundamental Java language classes. Note that the primitives start with a lower case letter, and the box objects start with an upper case. The upper/lower case naming convention is how we can distinguish between a primitive and a boxed object when we are reviewing unfamiliar code.

We can create boxed objects using the valueOf() method from the appropriate boxed object class, and then use one of the value conversion methods to obtain the value again. Converting a primitive to a boxed object is called 'wrapping', and converting a boxed object back to a primitive is called 'unwrapping'.

aa = Integer.valueOf(1);  // wrap 1 in a boxed object

if (aa.intValue() == 1)   // unwrap the value
   print("aa contains the value 1");

Wow, this looks inconvenient to do all of this wrapping and unwrapping. Fortunately Java and BeanShell will carry out automatic wrapping and unwrapping in many, but not all, situations. In fact, BeanShell has been enhanced to do more of this kind of automatic wrapping and unwrapping than Java:

aa = Integer.valueOf(1);  // wrap 1 in a boxed object

list = new ArrayList();
list.add(aa);
list.add(2);     // automatic wrapping
list.add(aa+2);  // automatic unwrap and wrap

print(list);  // prints out [1, 2, 3]

if (list.get(0) == 1)  // automatic unwrapping
   print("first list item is a 1");

Boxed objects have slightly slower performance characteristics than unwrapped primitives, primarily due to the extra object dereferencing and unwrapping that goes on behind the scenes. This performance loss is generally not an issue in scripting code, where run times a re brief compared to the long simulation times.