Java Reference
In-Depth Information
jects of the wrong type don't make it into your collections, and avoiding the need to write
casts when retrieving objects.
Table 7-2 shows some of the most important methods of ArrayList . Equally important,
those listed are also methods of the List interface, which we'll discuss shortly. This means
that the same methods can be used with the older Vector class and several other classes.
Table 7-2. List access methods
Method signature
Usage
Add the given element at the end
add(Object o)
Insert the given element at the specified position
add(int i, Object o)
Remove all element references from the Collection
clear( )
True if the List contains the given Object
contains(Object o)
Return the object reference at the specified position
get(int i)
Return the index where the given object is found, or -1
indexOf(Object o)
remove(Object o)remove(int i) Remove an object by reference or by position
Return an array containing the objects in the Collection
toArray( )
ArrayListDemo stores data in an ArrayList and retrieves it for processing:
public
public class
class ArrayListDemo
ArrayListDemo {
public
public static
void main ( String [] argv ) {
ArrayList < Date > al = new
static void
new ArrayList <>();
// Create a source of Objects
StructureDemo source = new
new StructureDemo ( 15 );
// Add lots of elements to the ArrayList...
al . add ( source . getDate ());
al . add ( source . getDate ());
al . add ( source . getDate ());
// Print them out using old-style for loop to index number.
Search WWH ::




Custom Search