// Program to demonstrate some basic usage of ArrayLists import java.util.*; // old operations: // String[] arr ==> ArrayList list // new String[n] ==> new ArrayList() // String s = arr[i]; ==> String s = list.get(i); // arr[i] = s; ==> list.set(i, s); // arr.length ==> list.size() // new operations: // list.add(s) - add a new element to the end of the list // list.add(i, s) - add a new element at the given index, // shifting later elements "right" // list.remove(i) - removes the element at the given index, // shifting later elements "left" // list.clear() - removes all elements of the list public class ArrayListExamples { public static void main(String[] args) { // create a list of words ArrayList words = new ArrayList(); words.add("may"); words.add("the"); words.add("odds"); words.add("ever"); words.add("in"); words.add("my"); words.add("uh...."); words.add("favor"); System.out.println(words); // fit it! words.add(3, "be"); words.remove(7); words.set(6, "your"); System.out.println(words); } }