In: Computer Science
Create a static method with the appropriate inputs and outputs. Call each of them in the main method.
Write a method named allMultiples() which takes in a List of integers and an int. The method returns a new List of integers which contains all of the numbers from the input list which are multiples of the given int. For example, if the List is [1, 25, 2, 5, 30, 19, 57, 2, 25] and 5 was provided, the new list should contain [25, 5, 30, 25].
Please do this in Java.
import java.util.ArrayList; import java.util.List; public class AllMultiplesList { public static List<Integer> allMultiples(List<Integer> list, int n){ List<Integer> result = new ArrayList<>(); for(int i = 0;i<list.size();i++){ if(list.get(i)%n==0){ result.add(list.get(i)); } } return result; } public static void main(String[] args) { List<Integer> list = new ArrayList<>(); list.add(1); list.add(25); list.add(2); list.add(5); list.add(30); list.add(19); list.add(57); list.add(2); list.add(25); List<Integer> result = allMultiples(list, 5); System.out.println(result); } }
[25, 5, 30, 25]