Answered step by step
Verified Expert Solution
Question
1 Approved Answer
TIP The for-each statement is not a safe structure for find- ing and removing elements from an ArrayList. TIP Java also includes the Character,
TIP The for-each statement is not a safe structure for find- ing and removing elements from an ArrayList. TIP Java also includes the Character, Boolean, Byte, Short, Long, and Float wrap- per classes. The following application creates an ArrayList object, adds elements, removes an element, and then displays the remaining elements: import java.util.ArrayList; public class TestArrayList { public static void main(String[] args) { ArrayList myStrings = new ArrayList (); nyStrings.add("Kermit"); nyStrings.add("Lucille"); myStrings.add("Sammy"); myStrings.add("Roxy"); myStrings.add("Myah"); myStrings.remove (3); for (String name: myStrings) System.out.println(name); The ArrayList declaration does not require an array size. An ArrayList object grows and shrinks automatically as elements are added and removed. The for-each statement traverses the ArrayList. The TestArrayList application displays the output: Kermit Lucille Sammy Myah Wrapper Classes Primitive data types cannot be directly stored in an ArrayList because the elements in an ArrayList must be objects. The Integer and Double classes, provided with Java, are used to "wrap" primitive values in an object. The Integer and Double wrapper classes include methods for com- paring objects and for returning, or "unwrapping", the primitive value stored by the object: Class Integer (java.lang.Integer) Method comparelo (Integer intObject) returns 0 when the Integer object value is the same as intObject. A negative int is returned when the Integer object is less than intObject, and a positive int is returned when the Integer object is greater than intObject. intValue() returns the int value of the Integer object. Chapter 10 Arrays 251 TIP The Integer and Double classes implement the Comparable interface, intro- duced in Chapter 9. 252 The Integer and Double wrapper classes are in the java.lang package. Therefore applications do not require an import statement to use the wrapper classes. Class Double (java.lang.Double) Method compareTo (Double doubleObject) The DataArrayList application creates an ArrayList of Integer values, compares the values, and then sums the elements: } returns 0 when the Double object value is the same as doubleObject. A negative int is returned when the Double object is less than doubleObject, and a positive int is returned when the Double object is greater than doubleObject. doubleValue() returns the double value of the Double object. import java.util.ArrayList; public class DataArrayList ( } public static void main(String[] args) { ArrayList The DataArrayList displays the output: TIP Autoboxing and auto- unboxing require JDK 5 or later. element1 value is greater than element2. Sum of the elements is: 7 Review: HighestGrade Create a HighestGrade application that prompts the user for five grades between 0 and 100 points and stores the grades in an ArrayList. HighestGrade then traverses the grades to determine the highest grade and then displays the grade along with an appropriate message. Autoboxing and Auto-Unboxing The use of primitives in an ArrayList requires additional code for wrapping and unwrapping values. The extra code can quickly "clutter" an application making it harder to read. Remembering the syntax for wrapping and unwrapping could also lead to longer development times. Autoboxing and auto-unboxing automatically wraps and unwraps primitives, eliminating the need for the extra code. The DataArrayList2 application creates an ArrayList of Integer values, compares the values, and then sums the elements without the extra wrapper class code: import java.util.ArrayList; public class DataArrayList2 { public static void main(String[] args) { ArrayList numbers = new ArrayList (); int elementi, element 2; int sum = 0; numbers.add(5); //autoboxing converts int 5 to Integer numbers.add (2); //autoboxing converts int 2 to Integer /* compare values */ element1 = numbers.get(0); element2 = numbers.get(1); if (element1 - element 2) { //auto-unboxing converts to int //auto-unboxing converts to int //primitives will be compared System.out.println("The elements have the same value."); } else if (element1 < element 2) { System.out.println("element1 value is less than element 2."); } else { System.out.println("element1 value is greater than element 2."); } /* sum values */ for (Integer num: numbers) { sun += num; //uses int value for sum } System.out.println("Sun of the elements is: The code above is cleaner and easier to understand. Autoboxing wraps, or boxes, a number in its appropriate wrapper class. Auto-unboxing calls the appropriate wrapper class method to unwrap, or unbox, the value stored in a wrapper class object. Chapter 10 Arrays 253
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started