Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Write the following program IN JAVA. 3+ * Removes all 0 elements from an ArrayList! import java.util.ArrayList; 7 public class RemoveZeroes 8. { public static

Write the following program IN JAVA.

* Removes all o elements from an ArrayList import java.util.ArrayList; public class RemoveZeroes { public static void main(St


 

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 a = new ArrayList (); 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 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: 14 19 3 15 18 44 51 78 83"); 38 39 40 41 42 43 public static ArrayList removezeros (ArrayList 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

Step by Step Solution

3.41 Rating (157 Votes )

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Mathematical Statistics With Applications In R

Authors: Chris P. Tsokos, K.M. Ramachandran

2nd Edition

124171133, 978-0124171138

More Books

Students also viewed these Chemical Engineering questions

Question

Solve each of the following equations. x + 0.07x = 64.20

Answered: 1 week ago