for each loop for ArrayList : ArrayList : java.util : Java by API examples (example source code) Organized by topic

Java by API
C++
PHP
Java by API Home »  java.util   » [  ArrayList  ]   
 



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.  ArrayList: add(E o)
3.  ArrayList: add(int index, E element)
4.  ArrayList: contains(Object elem)
5.  ArrayList: ensureCapacity(int minCapacity)
6.  ArrayList: indexOf(Object elem)
7.  ArrayList: iterator()
8.  ArrayList: listIterator()
9.  ArrayList: remove(int index)
10.  ArrayList: remove(Object o)
11.  ArrayList: retainAll(Collection c)
12.  ArrayList: size()
13.  AbstractList: subList(int fromIndex, int toIndex)
14.  ArrayList: toArray(T[] a)
15.  ArrayList: trimToSize()
























Home| Contact Us
Copyright 2003 - 04 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.