ArrayLists and Iterators

Read this chapter about lists and the useful methods that are available for adding and inserting elements into a list. 

19. Wrapper Classes


Answer:

No.

The non-empty cells of an ArrayList must contain object references (or null).

Wrapper Classes

To put an int into an ArrayList, put the int inside of an Integer object. Now the object can be put into an ArrayList. The following program builds a list of integers and then writes them out.

ArrayListIntegersimport java.util.* ;
public class WrapperExample
{
  public static void main ( String[] args)
  {
    ArrayList<Integer> data = new ArrayList<Integer>();

    data.add( new Integer(1) );
    data.add( new Integer(3) );
    data.add( new Integer(17) );
    data.add( new Integer(29) );

    for ( Integer val : data )
      System.out.print( val + " " );

    System.out.println( );
  }
}

The program writes out:

1 3 17 29

The picture emphasizes that the ArrayList contains an array of object references, as always. It shows the ints each contained in a little box that represents the wrapper object.


Question 19:

Would this statement work in the program?

data.add( 44 );