Using java please help Write a method called stutter that accepts an ArrayList o
ID: 3844244 • Letter: U
Question
Using java please help
Write a method called stutter that accepts an ArrayList of Strings and an integer k as parameters. The method replaces every string in the list with k copies of that string. For example, if the list stores the values ["how", "are", "you"] before the method is called, and k is 4, it should store the values ["how", "how", "how", "how", "are", "are", "are", "are", "you", "you", "you", "you"] after the method finishes executing. If k is 0 or negative, the list should be empty after the call. Exercise 2: Write a method called interleave that accepts two ArrayLists of integers a1 and a2 as parameters and inserts the elements of a2 into a1 at alternating indexes. If the lists are of unequal length, the remaining elements of the longer list are left at the end of a1. For example, if a1 stores [10, 20, 30] and a2 stores [4, 5, 6, 7, 8], the call of interleave (a1, a2); should change a1 to store [10, 4, 20, 5, 30, 6, 7, 8]. If a2 had stored [10, 20, 30, 40, 50] and a2 had stored [6, 7, 8], the call of interleave (a1, a2); would change a1 to store [10, 6, 20, 7, 30, 8, 40, 50].Explanation / Answer
Well...two things are wrong here:
I won't give the entire thing away, as this is an exercise for you, but here's some suggestions:
Create your own ArrayList<String> to return instead of that String variable. You'll also be declaring the method to return ArrayList<String>. May as well initialize it, too.
Read each word in the list passed in. Add that to the local list k times (hint: nested loops). If there's no words to be read, then the loop to add the elements isn't fired.
Related Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.