for each loop for ArrayList : ArrayList « java.util « Java by API






for each loop for ArrayList

  
/**
 *Output: 
Original contents of vals: 1 2 3 4 5 
Sum of values: 15
 */

import java.util.ArrayList;

public class MainClass {
  public static void main(String args[]) {
    ArrayList<Integer> vals = new ArrayList<Integer>();

    vals.add(1);
    vals.add(2);
    vals.add(3);
    vals.add(4);
    vals.add(5);

    System.out.print("Original contents of vals: ");
    for (int v : vals)
      System.out.print(v + " ");
    System.out.println();

    int sum = 0;
    for (int v : vals)
      sum += v;

    System.out.println("Sum of values: " + sum);
  }
}

           
         
    
  








Related examples in the same category

1.new ArrayList < E > ()
2.new ArrayList(Collection c)
3.ArrayList: add(E o)
4.ArrayList: add(int index, E element)
5.ArrayList: addAll(Collection c)
6.ArrayList: addAll(int index, Collection c)
7.ArrayList: clear()
8.ArrayList: contains(Object elem)
9.ArrayList: equals(Object o)
10.ArrayList: ensureCapacity(int minCapacity)
11.ArrayList: get(int index)
12.ArrayList: indexOf(Object elem)
13.ArrayList: isEmpty()
14.ArrayList: iterator()
15.ArrayList: lastIndexOf(Object o)
16.ArrayList: listIterator()
17.ArrayList: remove(int index)
18.ArrayList: remove(Object o)
19.ArrayList: removeAll(Collection c)
20.ArrayList: retainAll(Collection c)
21.ArrayList: set(int index, T element)
22.ArrayList: size()
23.AbstractList: subList(int fromIndex, int toIndex)
24.ArrayList: toArray()
25.ArrayList: toArray(T[] a)
26.ArrayList: trimToSize()