SHARE
TWEET

ListRemoveOdd

xboy90 Feb 25th, 2020 75 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package CodeAcademy_6_Arrays_Functions;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.List;
  5.  
  6. public class ListRemoveOdd
  7. {
  8.   public static void main(String[] args)
  9.   {
  10.  
  11.     // Remove all elements at odd indexes in the ArrayList
  12.  
  13.     ArrayList<Integer> nums = new ArrayList<>(List.of(1, 3, 5, 8, 9, 2, 12));
  14.  
  15.     for (int i = nums.size() - 1; i >= 0; i--) {
  16.       if (nums.indexOf(i % 2) == 0) {
  17.         System.out.println("Removed at index - " + i + ". Number removed - " + nums.remove(i));
  18.       }
  19.     }
  20.  
  21.     System.out.println("ArrayList after removal " + nums);
  22.  
  23.   }
  24. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top