How to sum numbers in ArrayList only those not between x and y with Java

1 Answer

0 votes
import java.util.ArrayList;
import java.util.Arrays;
 
public class SumNumbersInArrayListOnlyThoseNotBetweenXandY_Java {
    public static void main(String[] args) {
        ArrayList<Integer> alist = new ArrayList<Integer>(Arrays.asList(1, 4, 9, 3, 6, 2, 3, 2));
 
        int x = 2;
        int y = 4;
         
        int i = 0, total = 0;
        while (i < alist.size()) {
            if (!(alist.get(i) >= x && alist.get(i) <= y)) { // 1 9 6 
                total += alist.get(i);
            }
            i++;
        }
         
        System.out.println(total);
    }
}
 
    
    
/*
run:
     
16
     
*/

 



answered Aug 18, 2024 by avibootz
edited Aug 18, 2024 by avibootz
...