Complete the tuna method Zeros(). This method modifies the original list to add
ID: 3643566 • Letter: C
Question
Complete the tuna method Zeros(). This method modifies the original list to add a new node (with the value 0) after every node in the original list. For example, if the list originally contains the values 1, 2, 3, 4, 5in that order, after calling Zeros(), the list will contain the values
1, 0, 2, 0, 3, 0, 4, 0, 5, 0
Your solution must (eventually) modify the original list, and it should work with any list configuration (for
an empty list, just add a single node with the value 0).
Thank you ! Will Rate!
Explanation / Answer
please rate
import java.util.ArrayList;
public class ZeroPadding {
public void padWithZeros(ArrayList<Integer> mylist){
if(mylist==null)
return;
int size = mylist.size();
if(mylist.isEmpty()){
mylist.add(0);
}
for(int i=1;i<=(2*size);i+=2){
mylist.add(i, 0);
}
}
public static void main(String[] args){
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(2);
list.add(3);
new ZeroPadding().padWithZeros(list);
for(Integer i : list){
System.out.println(i.intValue());
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.