Example usage for java.util ArrayList ArrayList

List of usage examples for java.util ArrayList ArrayList

Introduction

In this page you can find the example usage for java.util ArrayList ArrayList.

Prototype

public ArrayList() 

Source Link

Document

Constructs an empty list with an initial capacity of ten.

Usage

From source file:Main.java

public static void main(String[] args) {
    // create vector object 
    List<String> vector = new ArrayList<String>();

    // populate the vector
    vector.add("1");
    vector.add("2");
    vector.add("3");
    vector.add("4");
    vector.add("from java2s.com");

    // create a synchronized view
    Collection<String> c = Collections.synchronizedCollection(vector);

    System.out.println("Sunchronized view is :" + c);
}

From source file:Main.java

public static void main(String[] args) {
    // create array list object
    List<Integer> numbers = new ArrayList<Integer>();

    // populate the list
    for (int i = 0; i < 15; i++) {
        numbers.add(i);//from   w  ww . j av  a  2s  .  c o  m
    }

    System.out.println("Before : " + Arrays.toString(numbers.toArray()));

    // rotate the list at distance 10
    Collections.rotate(numbers, 10);

    System.out.println("After : " + Arrays.toString(numbers.toArray()));
}

From source file:OldStyle.java

public static void main(String args[]) {
    ArrayList list = new ArrayList();

    // These lines store strings, but any type of object 
    // can be stored.  In old-style code, there is no  
    // convenient way restrict the type of objects stored 
    // in a collection 
    list.add("one");
    list.add("two");
    list.add("three");
    list.add("four");

    Iterator itr = list.iterator();
    while (itr.hasNext()) {

        // To retrieve an element, an explicit type cast is needed 
        // because the collection stores only Object. 
        String str = (String) itr.next(); // explicit cast needed here. 

        System.out.println(str + " is " + str.length() + " chars long.");
    }//www .  j a v a  2s  .  c  o  m
}

From source file:Main.java

public static void main(String args[]) {
    // create array list object       
    List<String> arrlist = new ArrayList<String>();

    // populate the list
    arrlist.add("A");
    arrlist.add("B");
    arrlist.add("from java2s.com");

    // create Enumeration
    Enumeration<String> e = Collections.enumeration(arrlist);

    while (e.hasMoreElements()) {
        System.out.println("Value is: " + e.nextElement());
    }//from  w  w w. j  a v a 2  s. com
}

From source file:UtilDemo3.java

public static void main(String[] args) {
    String[] coins = { "A", "B", "C", "D", "E" };

    List l = new ArrayList();
    for (int i = 0; i < coins.length; i++)
        l.add(coins[i]);//from  w w  w.  java2s. com

    ListIterator liter = l.listIterator();

    while (liter.hasNext())
        System.out.println(liter.next());

    Collections.reverse(l);

    liter = l.listIterator();

    while (liter.hasNext())
        System.out.println(liter.next());
}

From source file:Main.java

public static void main(String[] args) throws FileNotFoundException {
    List<Integer> arraySource = new ArrayList<>();
    for (int i = 0; i < 99999; i++) {
        arraySource.add(i);/*from w  w  w.  j a v a2  s. c o m*/
    }

    Integer[] myArray = new Integer[999];
    myArray = arraySource.toArray(myArray);
    long startTime = System.currentTimeMillis();
    Arrays.sort(myArray);
    long endTime = System.currentTimeMillis();
    System.out.println("Time take in serial: " + (endTime - startTime) / 1000.0);

    Integer[] myArray2 = new Integer[999];
    myArray2 = arraySource.toArray(myArray);
    startTime = System.currentTimeMillis();
    Arrays.parallelSort(myArray2);
    endTime = System.currentTimeMillis();
    System.out.println("Time take in parallel: " + (endTime - startTime) / 1000.0);

}

From source file:Main.java

public static void main(String[] args) {
    List<Integer> list = new ArrayList<>();
    long expectedSum = 0;
    for (int i = 0; i < 10000; i++) {
        int random = 1 + (int) (Math.random() * ((100 - 1) + 1));
        list.add(random);//w w w . j a va 2s. c om
        expectedSum += random;
    }
    System.out.println("expected sum: " + expectedSum);
    ForkJoinPool forkJoinPool = new ForkJoinPool(Runtime.getRuntime().availableProcessors());
    RecursiveSum recursiveSum = new RecursiveSum(list, 0, list.size());
    long recSum = forkJoinPool.invoke(recursiveSum);
    System.out.println("recursive-sum: " + recSum);
}

From source file:Main.java

public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    for (int i = 0; i < 30; i++) {
        list.add("Hello, World " + i);
    }/*from  www  .java 2s .co m*/
    JScrollPane pane = new JScrollPane(new JList(list.toArray())) {
        @Override
        public Dimension getPreferredSize() {
            return new Dimension(200, 250);
        }
    };
    JOptionPane.showMessageDialog(null, pane);
}

From source file:SafeCollectionIteration.java

public static void main(String[] args) {
    List wordList = Collections.synchronizedList(new ArrayList());

    wordList.add("Iterators");
    wordList.add("require");
    wordList.add("special");
    wordList.add("handling");

    synchronized (wordList) {
        Iterator iter = wordList.iterator();
        while (iter.hasNext()) {
            String s = (String) iter.next();
            System.out.println("found string: " + s + ", length=" + s.length());
        }//from  w w w.j a  v  a 2 s.c  o  m
    }
}

From source file:MainClass.java

public static void main(String[] args) {
    // Build a vector of words to be sorted
    ArrayList list = new ArrayList();
    list.add("m");
    list.add("c2");
    list.add("e");
    list.add("c1");

    Collator collate = Collator.getInstance();

    CollationKey[] keys = new CollationKey[list.size()];

    for (int k = 0; k < list.size(); k++)
        keys[k] = collate.getCollationKey((String) list.get(k));

    Arrays.sort(keys);//from w w w.ja v a2s.c om

    for (int l = 0; l < keys.length; l++) {
        System.out.println(keys[l].getSourceString());
    }
}