Answered step by step
Verified Expert Solution
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.
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
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