「java入門」java.util.ArrayList.get()のサンプルコード

Javaコード

import java.util.ArrayList;

public class ArrayListGetDemo {

public static void main(String[] args) {
// create an empty array list with an initial capacity
ArrayList<Integer> cflist = new ArrayList<Integer>(5);

// use add() method to add elements in the list
cflist.add(23);
cflist.add(24);
cflist.add(45);
cflist.add(86);

// let us print all the elements available in list
for (Integer number : cflist) {
System.out.println(“Value = ” + number);
}

// retrieves element at 4th postion
int retval=cflist.get(3);
System.out.println(“Retrieved element is = ” + retval);
}

}

結果

Value = 23
Value = 24
Value = 45
Value = 86
Retrieved element is = 86