Write a method manyStrings that takes an ArrayList of Strings and an integer n a
ID: 3541750 • Letter: W
Question
Write a method manyStrings that takes an ArrayList of Strings and an integer n as parameters and that replaces every String in the original list with n of that String. For example, suppose that an ArrayList called "list" contains the following values:
("squid", "octopus")
And you make the following call:
manyStrings(list, 2);
Then list should store the following values after the call:
("squid", "squid", "octopus", "octopus")
As another example, suppose that list contains the following:
("a", "a", "b", "c")
and you make the following call:
manyStrings(list, 3);
Then list should store the following values after the call:
("a", "a", "a", "a", "a", "a", "b", "b", "b", "c", "c", "c")
You may assume that the ArrayList you are passed contains only Strings and that the integer n is greater than 0.
Explanation / Answer
please rate - thanks
any questions ask
import java.util.*;
public class main
{public static void main(String []args)
{ArrayList<String> thing1 = new ArrayList<String>();
ArrayList<String> thing2 = new ArrayList<String>();
thing1.add("squid");
thing1.add("octopus");
thing2.add("a");
thing2.add("a");
thing2.add("b");
thing2.add("c");
System.out.println("before call : "+thing1);
manyStrings(thing1,2);
System.out.println("after call : "+thing1);
System.out.println("before call : "+thing2);
manyStrings(thing2,3);
System.out.println("call call : "+thing2);
}
public static void manyStrings(ArrayList<String>list,int n)
{int i,j,index,len;
String value;
len=list.size();
for(i=0;i<len;i++)
{index=i*n;
value=list.get(index);
for(j=0;j<n-1;j++)
list.add(index+j+1,value);
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.