{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}


Java as a test program by adding new method calls to

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: ogram in which the method is used. You could, for example, use ArrayListDemo.java as a test program by adding new method calls to it. In addition to the actual ArrayList methods, you can use the static methods of class Collections when you work with ArrayList arrays. Some of the Collections methods are used in the programs of this section. In the javafilesextra folder, there is the program CollectionsMethods.java which demonstrates some of the Collections methods. B B C D D E addAll() operation F E F G G H w I x J y is performed for this array. All the array elements of the array referenced by array_to_insert are copied to these positions in the destination array. C z array_to_modify.addAll( 7, array_to_insert ) ; A A In this drawing we suppose that this array is referenced by array_to_modify, and the operation 15.1 ArrayList class H I w This is the array referenced by array_to_insert. This array is inserted into the position with index 7 in the other array. J These elements of the array referenced by array_to_modify are moved to positions with larger indexes. w x x y y z z Figure 15-1. Performing an addAll() operation with ArrayList arrays. "Old-fashioned" ArrayList arrays In older Java versions the ArrayList class was not a generic class. This means that ArrayList-based arrays were declared without specifying the type of the objects that were intended to be stored to the array. To be compatible with older Java versions, the latest Java versions still accept ArrayList arrays that are created without a type parameter. For example, the array referenced by miscellaneous_objects, that is discussed in this section, can be declared and created with the statement ArrayList miscellaneous_objects = new ArrayList() ; This array works in the same way as an array that is created with the statement ArrayList<Object> miscellaneous_objects = new ArrayList<Object>() ; but an ArrayList array that is declared without a type parameter is less reliable in some situations, and, therefore, such arrays should not be used. Also the compiler prints warning messages when such arrays are declared. 505 506 Chapter 15: More standard Java types omeDtsiLyarrA avaj>3selifavaj\:D Here, method remove() has removed the array element in position with index 4. Another version of remove() has been called to remove value 888 from the array. 3 :xedni sah 404 eulaV 404 404 999 505 999 404 One version of the addAll() method adds the elements of another array to the end of an ArrayList array. Here it has added the 2 elements from the array referenced by another_array. 404 505 888 888 101 101 202 202 202 777 777 666 404 114 999 999 202 101 777 666 666 999 101 101 101 202 202 ArrayListDemo.java - X. Experiments with ArrayList-based arrays. // ArrayListDemo.java import java.util.* ; The size() method knows how many elements an ArrayList array currently contains. class ArrayListDemo { static void print_array( ArrayList<Integer> given_array ) { System.out.print( "\n\n " ) ; for ( int element_...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online