JAVA

Write a method that takes an ArrayList of Strings, and an int numRepeats and returns a new ArrayList with the original ArrayList repeated numRepeats times.


For example if we had an ArrayList list with the values ["a", "b", "c"]:


repeatArrayList(list, 3)

Should return a new ArrayList with the elements:


["a", "b", "c", "a", "b", "c", "a", "b", "c"]


public ArrayList repeatArrayList(ArrayList list, int numRepeats)

{

}

Respuesta :

Answer:

Explanation:

The following code is written in Java. It creates a function that takes in an ArrayList and an int parameter. Then it loops through the array and adds each element into a new array called newList but adds them the number of times that the numRepeats argument states. Output can be seen in the attached image below.

import java.util.ArrayList;

class Brainly{

   public static void main(String[] args) {

       ArrayList list = new ArrayList();

       list.add('a');

       list.add('b');

       list.add('c');

       ArrayList newList = repeatArrayList(list, 3);

       System.out.println(newList);

   }

   public static ArrayList repeatArrayList(ArrayList list, int numRepeats) {

       ArrayList newList = new ArrayList();

       for (int x = 0; x < numRepeats; x++) {

           for (int i = 0; i < list.size(); i++) {

               newList.add(list.get(i));

           }

       }

       return newList;

   }

}

Ver imagen sandlee09
ACCESS MORE
EDU ACCESS