Write a C++ recursive method doubleElements that has one parameter, an ArrayList
ID: 3768325 • Letter: W
Question
Write a C++ recursive method doubleElements that has one parameter, an ArrayList of strings. This method modifies the ArrayList by replacing every string with two copies of that string. For example, if the list has the values {"recursion", "is", "cool"} before the method is called, it should have the values {"recursion", "recursion", "is", "is", "cool", "cool"}. You may assume that the ArrayList specified is not null. You may not create any new ArrayLists or arrays. HINT: You will need a helper method that will be the recursive method.
Explanation / Answer
import java.util.*;
class word_copy_arrayList
{
public static void main(String args[])
{
int i,n;
String s;
ArrayList<String> list = new
ArrayList<String>();
list.add("aaa");
list.add("bbb");
list.add("ccc");
n=list.size();
for(i=0;i<=n;i=i+2)
{
list.add(i+1,list.get(i));
}
n=list.size();
for(i=0;i<n;i++)
{
System.out.println(list.get(i));
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.