Size elementindex systemoutprintf 5s givenarrayget

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: index = 0 ; element_index < given_array.size() ; element_index ++ ) { System.out.printf( "%5s", given_array.get( element_index ) ) ; } } Method print_array() prints the array versions that are created in method main(). When the array elements are printed with the format specifier %5s, they appear right-justified on the screen. The s in the format specifier refers to the word "string". When the elements are printed as strings, the toString() method is automatically invoked for the objects. An element of an ArrayList array can be read with the get() method which takes an index as a parameter. - 1: Demonstrating the standard class ArrayList. 15.1 ArrayList class Here, the standard class ArrayList is used to create an array. As the type parameter is <Integer>, the array will store Integer objects. It is not necessary to specify any length or size for the array. Initially, this array does not contain any elements, but the array grows automatically when objects are added to it. Method add() pushes an object to the end of the array. An add() operation increases the number of array elements by one. The int values that are given to the add() method are automatically boxed inside Integer objects. public static void main( String not_in_use ) { ArrayList<Integer> array_of_integers = new array_of_integers.add( array_of_integers.add( array_of_integers.add( array_of_integers.add( 202 101 505 404 ) ) ) ) ArrayList<Integer>() ; ; ; ; ; System.out.print( "\n Value 404 has index: " + array_of_integers.indexOf( 404 ) print_array( array_of_integers ) ; ); array_of_integers.add( 2, 999 ) ; array_of_integers.add( 2, 888 ) ; print_array( array_of_integers ) ; array_of_integers.remove( 4 ) ; print_array( array_of_integers ) ; array_of_integers.remove( new Integer( 888 ) ) ; print_array( array_of_integers ) ; ArrayList<Integer> another_array another_array.add( 777 ) ; another_array.add( 666 ) ; print_array( another_array ) ; = new ArrayList<Integer>() ; array_of_integers.addAll( another_array ) ; print_array( array_of_integers ) ; array_of_integers.set( 3, array_of_integers.get( 3 ) print_array( array_of_integers ) ; + 7); } } With method get() it is possible to read an array element in certain array position, and the set() method writes a new value to a specified array position. As the index values start from zero, 3 refers to the fourth array element. This statement thus adds 7 to the fourth element. Another version of the add() method is used to insert new array elements to the array position with index 2. As a result of the insertion operation, the elements in positions with indexes 2, 3, 4, ... are moved to positions with indexes 3, 4, 5, ..., respectively. - 2. Using the methods of class ArrayList to modify arrays. 507 508 Chapter 15: More standard Java types Iterators Many standard classes in the Java class library provide the possibility to use so-called iterators. For example, class ArrayList has inherited a method named iterator() that returns an iterator that can...
View Full Document

This document was uploaded on 01/20/2014.

Ask a homework question - tutors are online