1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36
| package divisible;
import java.util.Arrays;
public class Divisible { public int[] divisible(int[] array, int divisor) {
int div = 0; for (int i = 0; i < array.length; i++) { if (array[i] % divisor == 0) { div++; } } int[] ret = new int[div]; int j = 0; for (int i = 0; i < array.length; i++) { if (array[i] % divisor == 0) { ret[j] = array[i]; j++; } }
return ret; }
public static void main(String[] args) { Divisible div = new Divisible(); int[] array = { 5, 9, 7, 10 }; System.out.println(Arrays.toString(div.divisible(array, 5))); } }
|