Write the following program IN JAVA. 3+ * Removes all 0 elements from an ArrayList! import java.util.ArrayList;
Fantastic news! We've Found the answer you've been seeking!
Question:
Write the following program IN JAVA.
Transcribed Image Text:
3+ * Removes all 0 elements from an ArrayList! import java.util.ArrayList; 7 public class RemoveZeroes 8. { public static void main(String[] args) { 9. 10 ArrayList<Integer> a = new ArrayList<Integer> (); 11 12 // Add some integers to the array list a.add (14); a.add (0); a.add(19); a.add (3); a.add (15); a.add (0); a.add(18); a.add(0); a.add (44); a.add (0); a.add(51); a.add(78); // You can also create an Integer wrapper explicitly and add to the array list a.add (new Integer(83)); 13 14 15 16 17 18 19 // Print the array list - Note the use of the size() method and the get() method System.out.println("Before removing the 0 elements:"); for (int i = 0; i < a.size(); i++) { System.out.print(a.get (i) + " "); 20 21 22 23 24 25 26 System.out.println(); 27 28 // Remove the 0 elements ArrayList<Integer> aNoZeros = removeZeros (a); 29 30 31 // Print ArrayList a again to see new elements. System.out.print ln("After removing the 0 elements:"); for (int i = 0; i < aNoZeros.size(); i++) { System.out.print(aNoZeros.get(i) + 32 33 34 35 36 " "); 37 System.out.print ln(); System.out.println("Expected:\n14 19 3 15 18 44 51 78 83"); 38 39 40 41 42 43 public static ArrayList<Integer> removezeros (ArrayList<Integer> p) { // The best way: Create a new empty integer array list and // only copy the non-zero numbers of parameter array list p into it. // Use a for loop //----------Start below here. To do: approximate lines of code = 5 44 45 46 47 48 49 // 50 51 52 53 54 55 56 57 58 // Return the reference to the newly created array list 59 60 --End here. Please do not remove this comment. Reminder: no changes outside the todo regions. 61 62 63 64 3+ * Removes all 0 elements from an ArrayList! import java.util.ArrayList; 7 public class RemoveZeroes 8. { public static void main(String[] args) { 9. 10 ArrayList<Integer> a = new ArrayList<Integer> (); 11 12 // Add some integers to the array list a.add (14); a.add (0); a.add(19); a.add (3); a.add (15); a.add (0); a.add(18); a.add(0); a.add (44); a.add (0); a.add(51); a.add(78); // You can also create an Integer wrapper explicitly and add to the array list a.add (new Integer(83)); 13 14 15 16 17 18 19 // Print the array list - Note the use of the size() method and the get() method System.out.println("Before removing the 0 elements:"); for (int i = 0; i < a.size(); i++) { System.out.print(a.get (i) + " "); 20 21 22 23 24 25 26 System.out.println(); 27 28 // Remove the 0 elements ArrayList<Integer> aNoZeros = removeZeros (a); 29 30 31 // Print ArrayList a again to see new elements. System.out.print ln("After removing the 0 elements:"); for (int i = 0; i < aNoZeros.size(); i++) { System.out.print(aNoZeros.get(i) + 32 33 34 35 36 " "); 37 System.out.print ln(); System.out.println("Expected:\n14 19 3 15 18 44 51 78 83"); 38 39 40 41 42 43 public static ArrayList<Integer> removezeros (ArrayList<Integer> p) { // The best way: Create a new empty integer array list and // only copy the non-zero numbers of parameter array list p into it. // Use a for loop //----------Start below here. To do: approximate lines of code = 5 44 45 46 47 48 49 // 50 51 52 53 54 55 56 57 58 // Return the reference to the newly created array list 59 60 --End here. Please do not remove this comment. Reminder: no changes outside the todo regions. 61 62 63 64
Expert Answer:
Related Book For
Mathematical Statistics with Applications in R
ISBN: 978-0124171138
2nd edition
Authors: Chris P. Tsokos, K.M. Ramachandran
Posted Date:
Students also viewed these chemical engineering questions
-
Write a Java program that simulates car racing, as shown in Figure 16.22(a). The car moves from left to right. When it hits the right end, it restarts from the left and continues the same process....
-
Write a Java program to imitate a file system of an operating system. In your solution, design a class called BasicFile with options to carry out the following operations: (a) Select and open an...
-
Write a Java program that displays a frame window 300 pixels wide and 200 pixels high with the title My First Frame. Place the frame so that its top left corner is at a position 50 pixels from the...
-
The inequality describes the range of monthly average temperatures T in degrees Fahrenheit at a certain location. (a) Solve the inequality. (b) If the high and low monthly average temperatures...
-
A researcher used stepwise regression to create regression models to predict BirthRate (births per 1,000) using five predictors: LifeExp (life expectancy in years), InfMort (infant mortality rate),...
-
This problem exercises the basic concepts of game playing, using tic-tac-toe (noughts and crosses) as an example. We define Xn as the number of rows, columns, or diagonals with exactly n Xs and no Os...
-
Why do the terms of the attorney-client relationship need to be established in writing?
-
The following are costs associated with manufacturing firms, merchandising firms, or service firms: a. Miscellaneous materials used in production b. Salespersons commission in a real estate firm c....
-
What is the operations performance at operational level and any two trade-offs in operations performance objectives of Domino's Pizza?
-
Joy Wu, a PA, is planning her first audit of a closely held small business. In prior years, Wu compiled the financial statements of the company. She also helped to set up its accounting system and...
-
Division X of Charter Corporation makes and sells a single product which is used by manufacturers of fork lift trucks. Presently it sells 12,000 units per year to outside customers at $24 per unit....
-
Matilda works for a company with 1,000 employees. The company has a hospitalization insurance plan that covers all employees. However, the employee must pay the first $3,000 of their medical expenses...
-
Research how companies are measuring performance and reporting it in modern HR systems. How have companies moved to use better assess employee productivity?
-
The FallSafe Parachute Factory has charged you to calculate itstotal costs for the preceeding six months. The company trusts your professional judgement, so you have the freedom to choose between...
-
Theories of Justice There is a universal appeal to justice. All persons are aware of the need for justice, yet there is no agreement on the nature of justice or what arrangements constitute a just...
-
Read the following case scenario: You are working as a clinical mental health counselor at a high school in the United States. The school's demographic is mostly Latino students. He is running a...
-
Assessment 3 Information Subject Code: ACCM6000 Subject Name: Capstone Assessment Title: Assessment Type: Word Count: Weighting: Total Marks: Submission: Due Dates: Your Task Literature Review...
-
Calculate the Lagrange polynomial P 2 (x) for the values (1.00) = 1.0000, (1.02) = 0.9888, (1.04) = 0.9784 of the gamma function [(24) in App. A3.1] and from it approximations of (1.01) and (1.03).
-
Let X1, . . ., Xn be a random sample from a two-parameter exponential population with density Find MLEs for and when both are unknown. 1 f(x,0,u)-Te-T, for x20, >0.
-
A survey indicates that it is important to pay attention to truth in political advertising. Based on a survey of 1200 people, 35% indicated that they found political advertisements to be untrue; 60%...
-
Let X1, . . ., Xn be a random sample from a negative binomial distribution with pmf Find method of moments estimators for r and p. [Here E[X] = r(1 p) / p and E[X2] = r(1 p) (r rp + 1) / p2.] p(x,...
-
Under what conditions can WTO members not use MFN when dealing with one another?
-
What does most favored nation (MFN) mean?
-
How do the various forms of economic integration differ?
Study smarter with the SolutionInn App