TIP The for-each statement is not a safe structure for find- ing and removing elements from...
Fantastic news! We've Found the answer you've been seeking!
Question:
Transcribed Image Text:
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 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 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 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 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
Expert Answer:
Related Book For
Posted Date:
Students also viewed these programming questions
-
QUIZ... Let D be a poset and let f : D D be a monotone function. (i) Give the definition of the least pre-fixed point, fix (f), of f. Show that fix (f) is a fixed point of f. [5 marks] (ii) Show that...
-
This question concerns lexical grammars. (a) Tree Adjoining Grammars contain two types of elementary tree. (i) What are these trees called? [1 mark] (ii) If one were building a grammar for English...
-
Put all of the below items in the correct format. Don't be concerned about numbers. Just get the lines and headings all in the right places. Beginning Cash Balance CASH FLOWS FROM FINANCING...
-
Nautical Marina needs to raise $1.0 million to expand the company. Nautical Marina is considering the issuance of either $1,000,000 of 8% bonds payable to borrow the money, or 100,000 shares of...
-
How has trade survived covid-19? Note: read the article and write the summary answering the question above. https://www.economist.com/finance-and-economics/2020/09/12/how-has-trade-survived-covid-19
-
What are the ethical obligations in using social media sites in litigation?
-
Selected accounts from the ledgers of Lockhart Company at July 31 showed the following. InstructionsFrom the data prepare:(a) The single-column purchases journal for July.(b) The general journal...
-
You are considering investing in one of two different projects. Two investment options are available as follows: Projekt A, million $ Projekt B, million $ (Basic Investment) 1200 75 Net cash flow,...
-
Given the following sketches, generate an Excel spreadsheet: 1) Count the total degrees of freedom in the sketch. 2) Count the constraints 3) Provide the number of dimensions that are necessary to...
-
Case Study: Bounce Fitness Group Bounce Fitness Company has now accepted that increasing their cash flow is important for sustained growth and to support a wider strategic goal of global market...
-
How has GE unified its various brands under one marketing umbrella (or brand).? Based on the brand: Adidas running shoes. You need to answer to the following questions: Q1. Identify the Brand's media...
-
Explain why you chose tosave $2,000 for a vacationover a time frame oftwo years. (Your response should be 2-3 sentences.)Remember, if you use any information or ideas from the webtext, be sure to add...
-
A small object, which has a charge q = 5.7 C and mass m = 2.1 x 105 kg, is placed in a constant electric field. Starting from rest, the object accelerates to a speed of 3.3 x 103 m/s in a time of 1.1...
-
What strategies can be employed to reduce stress and burnout among healthcare professionals during prolonged periods of crisis ?
-
In exchange for a 40% interest in ABC Partnership, Faith contributes land with a basis of $50,000 and FMV of $125,000. There is a mortgage on the land for $30,000 which ABC Partnership assumes....
-
1) Given: X = 0xA4 and Y = 0x95, a) Convert X and Y to 8-bit binary numbers. b) Compute the 8-bit sum X+Y of X and Y. c) Compute the 8-bit two's complement of Y. d) Compute the 8-bit difference X-Y...
-
Avatar Financials, Inc., located on Madison Avenue, New York City, is a company that provides financial advice to individuals and small- to mid-sized businesses. Its primary operations are in wealth...
-
Write an attribute grammar whose base BNF is that of Example 3.2 and whose type rules are the same as for the assignment statement example of Section 3.4.5. Data From Example 3.2: A| BIC + | * |(...
-
What mixed-mode assignments are allowed in ML?
-
There have been some public interchanges between Microsoft and Sun concerning the design of Microsofts J++ and C# and Suns Java. Read some of these documents, which are available on their respective...
-
The upper curve in the boiling point diagram is called (a) The saturated vapour curve (b) The dew point curve (c) The saturated liquid curve (d) Both (a) and (b).
-
A vertical cylinder containing helium gas is filled with a piston of \(50 \mathrm{~kg}\) mass and crosssectional area of \(0.025 \mathrm{~m}^{2}\). If the atmospheric pressure outside the cylinder is...
-
The Joule-Thomson coefficient for any gas at inversion point is (a) 1 (b) 0 (c) 2 (d) 3
Study smarter with the SolutionInn App