Pacific-Design.com

    
Home Index

1. Algorithms

2. ArrayList

Algorithms / ArrayList /

ArrayList

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

public class TestArrayList {

    public static void main(String[] args) {
        
        List<String> list = new ArrayList<String>();
        list.add("Kevin");
        list.add("Thomas");
        list.add("Duraj");

        /*----------------- For Loop ----------------*/
        for (int i = 0; i < list.size(); i++) {
            System.out.println(list.get(i));
        }

        /*--------------- ForEach Loop --------------*/
        for (String temp : list) {
            System.out.println(temp);
        }

        /*---------------- While Loop ---------------*/
        int j = 0;
        while (list.size() > j) {
            System.out.println(list.get(j));
            j++;
        }

        /*---------------- Iterator Loop ---------------*/
        Iterator<String> iterator = list.iterator();
        while (iterator.hasNext()) {
            System.out.println(iterator.next());
        }
        
    }

}