Advertisement
karlakmkj

Iterate ArrayList

Dec 2nd, 2020
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.54 KB | None | 0 0
  1. import java.util.ArrayList;
  2.  
  3. class CalculateTotal {
  4.  
  5.   public static void main(String[] args) {
  6.    
  7.     ArrayList<Double> expenses = new ArrayList<Double>();
  8.     expenses.add(74.46);
  9.     expenses.add(63.99);
  10.     expenses.add(10.57);
  11.     expenses.add(81.37);
  12.    
  13.     double total = 0;
  14.    
  15.     // Iterate over expenses for ArrayList
  16.     for (int i=0; i<expenses.size(); i++){
  17.         total = total + expenses.get(i); //get value of each item in the list and add it to the total
  18.     }
  19.    
  20.     System.out.println(total);
  21.    
  22.   }
  23. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement