A13. Exercises 3

  1. Make an ArrayList with 20 elements, all zero except a 1 at indices 5, 10, and 15.

     public static ArrayList<Integer> mk1 ()
    
  2. (kill4) Remove all of the multiples of 4 from an ArrayList. Change multiples of 10 to half of their original value if the remain in the output.

     public static void kill4(ArrayList<Integer> nums)
    
  3. (filterx) Make a new List with all of elements from the input that are bigger than x. Do not change the original.

     public static ArrayList<Integer> filterx (ArrayList<Integer> nums, int x)
    

Test code

public static void testit() {
  ArrayList<Integer> two = new ArrayList<>();
  two.add(8); two.add(4); two.add(15); two.add(10); two.add(0);
  two.add(-4); two.add(66); two.add(30); two.add(20);
  
  ArrayList<Integer> ta = new ArrayList<>();
  ta.add(15); ta.add(5) ta.add(66); ta.add(15); 
  
  kill4(two);
  System.out.println("Good? "+ta.equals(two));
}