chapter_15_arraylists_javabook

Chapter_15_arraylists_javabook

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: lements the array contains. Because ArrayList arrays have, at least in theory, an unlimited capacity to store data, ArrayList arrays do not have the length field. Instead, the ArrayList class provides the size() method which returns a value that tells how many elements an array currently contains. ArrayList-based arrays cannot be indexed with index expressions inside square brackets. To access individual array elements, the ArrayList class provides methods named get() and set(). With these methods an element in certain index position can be read or written. We get a particularly interesting ArrayList-based array with the statement ArrayList<Object> miscellaneous_objects = new ArrayList<Object>() ; This statement creates an array that can store objects of the standard class Object and objects of all subclasses of the Object class. Because Object is the superclass of all Java classes, the array created by the above statement can store all kinds of objects. All the following statements would thus be acceptable: miscellaneous_objects.add( miscellaneous_objects.add( miscellaneous_objects.add( miscellaneous_objects.add( 555 ) ; 66.77 ) ; "This is a string literal." ) ; new Date( "03.02.2004" ) ) ; These statements add objects of types Integer, Double, String, and Date to the same array, and each element of the array points to a different type of object. It is easy to add different kinds of objects to the above array, but when we want to do something with the objects, the situation can be somewhat more complicated. For example, if we want to take a substring that contains the first three characters of the String object that is the third one added to the above array, we might first write miscellaneous_objects.get( 2 ).substring( 0, 3 ) The compiler would not, however, accept this because the method call get( 2 ) returns a reference to type Object, and class Object does not have a method named substring(). In order to make the above method call acceptable to the compiler, we would have to convert the array element to type String in the following way ((String) miscellaneous_objects.get( 2 )).substring( 0, 3 ) Things must not, fortunately, always be this difficult. For example, all the objects that are stored by the above array could be printed with the loop for ( int object_index = 0 ; object_index < miscellaneous_objects.size() ; object_index ++ ) { System.out.print( "\n " + miscellaneous_objects.get( object_index ) ) ; } 504 Chapter 15: More standard Java types In the case of the above loop, there are no problems with compilation because for all objects there is a method named toString(), and that method will be called automatically when the string concatenation operator (+) is applied to an object in the array. Inside the above loop, miscellaneous_objects.get( object_index ) returns an element of an ArrayList-based array, and that element is an object reference of type Object. When the string concatenation operator (+) is used to concatenate something to &q...
View Full Document

This document was uploaded on 01/20/2014.

Ask a homework question - tutors are online