Ask Question
30 September, 11:45

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.

+5
Answers (1)
  1. 30 September, 14:10
    0
    public static ArrayList manyStrings (ArrayList list, int n) {

    ArrayList newList = new ArrayList ();

    for (int i=0; i
    for (int j=0; j
    newList. add (list. get (i));

    }

    }

    return newList;

    }

    Explanation:

    Create a method called manyStrings that takes two parameters, list and n

    Create a new ArrayList that will hold new values

    Create a nested for loop. The outer loop iterates through the list. The inner loop adds the elements, n of this element, to the newList.

    When the loops are done, return the newList
Know the Answer?
Not Sure About the Answer?
Find an answer to your 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 ...” in 📘 Computers and Technology if you're in doubt about the correctness of the answers or there's no answer, then try to use the smart search and find answers to the similar questions.
Search for Other Answers