SHARE
SPREAD
HELP

The Tradition of Sharing

Help your friends and juniors by posting answers to the questions that you know. Also post questions that are not available.


To start with, Sr2Jr’s first step is to reduce the expenses related to education. To achieve this goal Sr2Jr organized the textbook’s question and answers. Sr2Jr is community based and need your support to fill the question and answers. The question and answers posted will be available free of cost to all.

 

#
Authors:
Stuart Reges, Marty Stepp
Chapter:
Arraylists
Exercise:
Exercises
Question:8 | ISBN:9780136091813 | Edition: 2

Question

Write a method called removeZeroes that takes as a parameter an ArrayList of integers and eliminates any occurrences of the number 0 from the list. For example, if the list stores the values (0, 7, 2, 0, 0, 4, 0) before the method is called, it should store the values (7, 2, 4) after the method finishes executing.

TextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbookTextbook

Answer

Implementation of removeZeroes method:

	public static void removeZeroes(ArrayList intList)
	{		
		for(int i = 0; i < intList.size(); i++)
		{	
			if(intList.get(i) == 0)
			{
				intList.remove(i);
				i--;
			}
		}
	}
0 0

Discussions

Post the discussion to improve the above solution.