No
To put an int
into an ArrayList
,
put the int
inside an of an Integer
object.
Mostly, the job of the wrapper class Integer
is to
provide objects that contain primitive data.
Now the objects (and the primitive data they contain) can be put into an ArrayList
or other data structure.
The following program builds a list of integers and then writes them out.
import java.util.* ; 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 ( int j=0; j < data.size(); j++ ) System.out.print( data.get(j) + " " ); 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 int
s each contained in a little box that
represents the wrapper object.
Would you expect the following to work?
data.add( 44 );