Write a method named repeatElements that accepts an ArrayList of strings and an integer "repeat factor" k as parameters. The method will change the ArrayList in which each element of the ArrayList will be copied k times. For example, if a variable called list contains the elements ["hi", "how are", "you?"], then the call of stretchElements(list, 3); will change the ArrayList to contain ["hi", "hi", "hi", "how are", "how are", "how are", "you?", "you?", "you?"]. Write Java statements that first get number of strings and then the strings from the user and add themto a list, also get the value of k from the user. Then print the ArrayList, call the method, then print the ArrayList again.
Write a method named repeatElements that accepts an ArrayList of strings and an integer "repeat factor" k as parameters. The method will change the ArrayList in which each element of the ArrayList will be copied k times. For example, if a variable called list contains the elements ["hi", "how are", "you?"], then the call of stretchElements(list, 3); will change the ArrayList to contain ["hi", "hi", "hi", "how are", "how are", "how are", "you?", "you?", "you?"]. Write Java statements that first get number of strings and then the strings from the user and add themto a list, also get the value of k from the user. Then print the ArrayList, call the method, then print the ArrayList again.
Note 1- If a stretch factor of 0 or less is passed, then the list is made empty. 2 - If an empty list is passed in, regardless of the stretch factor, the list should still be empty at the end of the call. You may assume that the list passed is not null. You may not use any other arrays, arraylists, or other data structures to help you solve this problem, though you can create as many simple variables as you like.
Trending now
This is a popular solution!
Step by step
Solved in 2 steps with 2 images