Rewrite lines 1025 in Fig. 16.3 to be more concise by using the asList method and the
Question:
Rewrite lines 10–25 in Fig. 16.3 to be more concise by using the asList method and the LinkedList constructor that takes a Collection argument.
Fig. 16.3
Transcribed Image Text:
1 // Fig. 16.3: ListTest.java // Lists, LinkedLists and ListIterators. 3 import java.util.List; 4 import java.util.LinkedList; 5 import java.util.ListIterator; 6 7 8 10 II 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 public class ListTest { public static void main(String[] args) { 72 73 74 75 76 77 78 } // add colors elements to list1 String[] colors= {"black", "yellow", "green", "blue", "violet", "silver"}; List list1= new LinkedList (); for (String color colors) { list1.add(color); } // add colors2 elements to list2 String[] colors2 = } {"gold", "white", "brown", "blue", "gray", "silver"}; List list2 = new LinkedList (); for (String color colors2) { list2.add(color); } list1.addAll(list2);// concatenate lists list2 = null; // release resources. printList (list1); // print listl elements convertToUppercaseStrings (list1); // convert to uppercase string printList (list1); // print list1 elements System.out.printf("%nDeleting elements 4 to 6..."); removeItems (list1, 4, 7); // remove items 4-6 from list printList (list1); // print list1 elements printReversedList (list1); // print list in reverse order } // output List contents private static void printList (List list) { System.out.printf("%nlist: %n"); for (String color : list) { System.out.printf( "%s", color); } System.out.println(); } // locate String objects and convert to uppercase private static void convertToUppercaseStrings (List list) { ListIterator iterator = list.listIterator(); while (iterator.hasNext()) { String color = iterator.next(); // get item iterator.set(color.toUpperCase()); // convert to upper case } } // obtain sublist and use clear method to delete sublist items private static void removeItems (List list, int start, int end) { list.subList(start, end).clear(); // remove items } // print reversed list private static void printReversed List (List list) { ListIterator iterator = list.listIterator(list.size()); System.out.printf("%nReversed List:%n"); // print list in reverse order while (iterator.hasPrevious()) { System.out.printf("%s ", iterator.previous()); } list: black yellow green blue violet silver gold white brown blue gray silver list: BLACK YELLOW GREEN BLUE VIOLET SILVER GOLD WHITE BROWN BLUE GRAY SILVER Deleting elements 4 to 6... list: BLACK YELLOW GREEN BLUE WHITE BROWN BLUE GRAY SILVER Reversed List: SILVER GRAY BLUE BROWN WHITE BLUE GREEN YELLOW BLACK
Fantastic news! We've Found the answer you've been seeking!
Step by Step Answer:
Answer rating: 100% (3 reviews)
To make the code more concise we can utilize the ArraysasList method which creates a fixedsize list ...View the full answer
Answered By
Raunak Agarwal
Teaching is my hobby and now my profession. I teach students of CA and CFA(USA) in batches of 100 students and have a 5 year experience.
5.00+
1+ Reviews
10+ Question Solved
Related Book For
Java How To Program Late Objects Version
ISBN: 9780136123712
8th Edition
Authors: Paul Deitel, Deitel & Associates
Question Posted:
Students also viewed these Computer science questions
-
We will continue working on Python. Using a Google Colab notebook write the python statement to define a Pizza class. The test harness is provided to you by the instructor as well as the resulting...
-
Choose a topic that can be applied to one of the following economic concepts. (Possible curves: Lorenz curve, marginal product of labor curve, labor demand and supply curves, utility function and...
-
The following additional information is available for the Dr. Ivan and Irene Incisor family from Chapters 1-5. Ivan's grandfather died and left a portfolio of municipal bonds. In 2012, they pay Ivan...
-
Last year, Intrepid Corporations tax return revealed the following items: dividends from 20 percent owned domestic corporations - $60,000; Gross income from services rendered - $300,000;...
-
Three expense recognition methods (associating cause and effect, systematic and rational allocation, and immediate recognition) were discussed in the text under the matching principle. Indicate the...
-
Average total daily sales at a small food store are known to be $452.80. The store's management recently implemented some changes in displays of goods, order within aisles, and other changes, and it...
-
From your own experience as a patient, provide an example of high-value healthcare.
-
Carlsbad Enterprises has a capacity to produce 400,000 computer cases per year. The company is currently producing and selling 320,000 cases per year at a selling price of $80 per case. The cost of...
-
Fill in the missing amount 85,000 units Work-in-process inventory, February 1 Work in process inventory, February 28 Units started during February Units completed and transferred during February...
-
Write a program that reads in a series of first names and eliminates duplicates by storing them in a Set. Allow the user to search for a first name.
-
Determine whether each of the following statements is true or false. If false, explain why. a) Vectors are synchronized by default. b) Class Stack does not have a method add. c) In order to use the...
-
Rework Problem 4b using this additional information: Expansion would result 1n an increase of $70,000 per year, subcontracting would result in an increase of $25,000 per year, and adding a new...
-
1. Make a comparison between the leadership approaches "Trait Models" and "Behavioral Models". Discuss the main postulates and differences between the models and provide examples of a theory...
-
Applying the Central Limit Theorem: The amount of contaminants that are allowed in food products is determined by the FDA ( Food and Drug Administration ) . Common contaminants in cow milk include...
-
A collection of techniques used by social scientists to compile, summarize, and convey numerical data. Revised Research Question Hypothesis : Null Hypothesis : A null hypothesis, often known as H0,...
-
Adidas is an international sporting apparel/shoes brand. If Adidas was to enter a new foreign market, it would conduct a country market assessment. Identify the 4 components of the assessment....
-
Using the scenario linked in the Supporting Materials section, assume that you are the cost accountant for your company, and the CFO has asked for your analysis on purchasing materials from an online...
-
Write an equation of the line perpendicular to the graph of the line 8x - y = 10 and containing the point (-1, 1).
-
A line l passes through the points with coordinates (0, 5) and (6, 7). a. Find the gradient of the line. b. Find an equation of the line in the form ax + by + c = 0.
-
Compare and contrast HDLC with PPP.
-
Compare the flag byte and the escape byte in PPP. Are they are the same? Explain.
-
In Figure 11.20, explain why we need only one address field. Explain why the address is set to the predefined value of (11111111) 2. Figure 11.20 (11111111)2 (00000011)2 Flag Address Control Protocol...
-
You have $55,000. You put 15% of your money in a stock with an expected return of 10%, $38,000 in a stock with an expected return of 18%, and the rest in a stock with an expected return of 22%. What...
-
Portfolio return and beta Personal Finance Problem Jamie Peters invested $ 1 1 3 , 0 0 0 to set up the following portfolio one year ago: a . Calculate the portfolio beta on the basis of the original...
-
. Emerson Cammack wishes to purchase an annuity contract that will pay him $7,000 a year for the rest of his life. The Philo Life Insurance Company figures that his life expectancy is 20 years, based...
Study smarter with the SolutionInn App