Question

In: Computer Science

Write a Java program that takes an ArrayList<Integer>,  adds k copies of it at the end, and...

Write a Java program that takes an ArrayList<Integer>,  adds k copies of it at the end, and returns the expanded ArrayList.  The total size will be (k+1) * n.  

public ArrayList<Integer> makeCopies(ArrayList<Integer>, int k) {

}

Example: ArrayList<Integer> has (3,7,4) and k = 2, then the returned, expanded ArrayList will have (3,7,4,3,7,4,3,7,4).  The total size is (k+1)*n = (2+1)*3= 9.

Solutions

Expert Solution

public ArrayList<Integer> makeCopies(ArrayList<Integer> list, int k) {
    ArrayList<Integer> result = new ArrayList<>(list);
    for (int i = 0; i < k; i++) {
        result.addAll(list);
    }
    return result;
}

import java.util.ArrayList;

public class ArrayListCopies {

    public ArrayList<Integer> makeCopies(ArrayList<Integer> list, int k) {
        ArrayList<Integer> result = new ArrayList<>(list);
        for (int i = 0; i < k; i++) {
            result.addAll(list);
        }
        return result;
    }

    public static void main(String[] args) {
        ArrayList<Integer> list = new ArrayList<>();
        list.add(3);
        list.add(7);
        list.add(4);
        System.out.println(list);
        System.out.println(new ArrayListCopies().makeCopies(list, 2));
    }
}


Related Solutions

JAVA - Write a program that creates an ArrayList and adds an Account object, a Date...
JAVA - Write a program that creates an ArrayList and adds an Account object, a Date object, a ClockWithAudio object, a BMI object, a Day object, and a FigurePane object. Then display all elements in the list. Assume that all classes (i.e. Date, Account, etc.) have their own no-argument constructor.
Write a program in C or in Java, that takes an integer value N from the...
Write a program in C or in Java, that takes an integer value N from the command line, generates N random points in the unit square, and computes the distance separating the closest pair of points. A unit square is a square with sides of length 1, at points (0, 0), (0, 1), (1, 0), and (1, 1). If you wish to avoid the command-line processing, you can just assume you will generate a fixed number of points, say between...
JAVA LANGUAGE ArrayList<Integer> al = new ArrayList<Integer>(); HashSet<Integer> hs = new HashSet<Integer>(); HashMap<Integer, Integer> hm =...
JAVA LANGUAGE ArrayList<Integer> al = new ArrayList<Integer>(); HashSet<Integer> hs = new HashSet<Integer>(); HashMap<Integer, Integer> hm = new HashMap<Integer,Integer>(); for (int i= 0; i<2; i++) { al.add(i); al.add(i+1); hs.add(i); hs.add(i+1); hm.put(al.get(i), al.get(i+1)); hm.put(hm.get(i), hm.get(i+1)); }   System.out.println(al); System.out.println(hs); System.out.println(hm); // {key=value}. ---------------------------------- What output is produced by the following code and why?
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that...
Java 1.Write a method removeEvenLength that takes an ArrayList of Strings as a parameter and that removes all of the strings of even length from the list. 2. Given the following Vehicle interface and client program in the Car class: public interface Vehicle{ public void move(); } public class Car implements Vehicle{ public static void main(String args[]) Vehicle v = new Vehicle(); // vehicle declaration } The above declaration is valid? True or False? 3. Java permits a class to...
Write a function that takes a numeric or integer vector and adds up only the numbers...
Write a function that takes a numeric or integer vector and adds up only the numbers whose integer parts are even. Modify your answer to question above to include an option that allows you to choose whether to sum numbers whose integer parts are even or are odd. Your function should have as a default that it gives the same output as the function in question 4. In other words, if the user doesn’t specify whether to sum evens or...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and...
Write Java code that accepts the integer input (from keyboard) in an arraylist named num1 and stores the even integers of num1 in another arraylist named evennum.
JAVA: when input is type ArrayList<ArrayList<Integer>> how to use java to get this solution [ [a,b,c]...
JAVA: when input is type ArrayList<ArrayList<Integer>> how to use java to get this solution [ [a,b,c] , [d,e], [f] ] ----> [ [a,d,f], [a,e,f], [b,d,f], [b,e,f], [c,d,f], [c,e,f]] [ [a,b], [a,b,c]] ----->[[a,a],[a,b],[a,c], [b,a],[b,b],[b,c] assuming abc are integer Thanks in advance!
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and...
java/ netbeans Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Compile and test your code in NetBeans and then on Hackerrank.
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input...
Code in Java Write a recursive method smallestNumber which takes an ArrayList of Integers as input and returns the smallest number in the array. You can use a helper method if needed. Write a main method that asks the user for a series of numbers, until the user enters a period. Main should create an ArrayList of these Integers and call smallestNumber to find the smallest number and print it. Input Format A series of integers Constraints None Output Format...
Write a program that takes in a positive integer as input, and outputs a string of...
Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binary. For an integer x, the algorithm is: As long as x is greater than 0 Output x % 2 (remainder is either 0 or 1) x = x / 2 Note: The above algorithm outputs the 0's and 1's in reverse order. Ex: If the input is: 6 the output is: 011 6 in binary is...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT