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");

    System.out.println("Before swap: " + vector);

    // swap the elements
    Collections.swap(vector, 0, 4);

    System.out.println("After swap: " + vector);
}

From source file:Main.java

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

    // populate the list
    arrlst.add("A");
    arrlst.add("B");
    arrlst.add("C");
    arrlst.add("D");
    arrlst.add("from java2s.com");

    System.out.println("The initial list is :" + arrlst);

    // reverse the list
    Collections.reverse(arrlst);//from   ww  w  .  j av a 2  s .  c  o  m

    System.out.println("The Reverse List is :" + arrlst);
}

From source file:IteratorDemo.java

public static void main(String args[]) {
    ArrayList<String> al = new ArrayList<String>();

    al.add("C");//  w  w w.  ja v  a  2 s. c  om
    al.add("A");
    al.add("E");
    al.add("B");
    al.add("D");
    al.add("F");

    Iterator<String> itr = al.iterator();
    while (itr.hasNext()) {
        String element = itr.next();
        System.out.print(element + " ");
    }

    ListIterator<String> litr = al.listIterator();
    while (litr.hasNext()) {
        String element = litr.next();
        litr.set(element + "+");
    }

    itr = al.iterator();
    while (itr.hasNext()) {
        String element = itr.next();
        System.out.print(element + " ");
    }

    while (litr.hasPrevious()) {
        String element = litr.previous();
        System.out.print(element + " ");
    }
}

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");

    System.out.println("Initial collection: " + arrlist);

    // shuffle the list
    Collections.shuffle(arrlist);

    System.out.println("Final collection after shuffle: " + arrlist);
}

From source file:MainClass.java

public static void main(String args[]) {
    ArrayList<Integer> vals = new ArrayList<Integer>();

    vals.add(1);/*from  w  w w  . j  a v  a  2s.c om*/
    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);
}

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");

    System.out.println("List elements before fill: " + arrlist);

    // fill the list
    Collections.fill(arrlist, "java2s.com");

    System.out.println("List elements after fill: " + arrlist);
}

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("B");
    arrlist.add("B");
    arrlist.add("B");
    arrlist.add("from java2s.com");

    // check frequensy of 'B'
    int freq = Collections.frequency(arrlist, "B");

    System.out.println("Frequency of 'B' is: " + freq);
}

From source file:Main.java

public static void main(String[] args) {
    List<Person> list = new ArrayList<Person>();
    for (int i = 10; i > 0; i--) {
        list.add(new Person(i, "name" + String.valueOf(i), new Date()));
    }//from w w  w.j a  v  a  2  s . c o m
    System.out.println(list);
    Collections.sort(list);
    System.out.println(list);
}

From source file:MainClass.java

public static void main(String[] av) {

    String[] stringArray = new String[] { "c", "e", "a", "k" };

    ArrayList<String> list = new ArrayList<String>();
    for (String s : stringArray) {
        list.add(s);/*w  w  w  .ja va2s . com*/
    }
    Collections.sort(list, String.CASE_INSENSITIVE_ORDER);

    for (String s : list) {
        System.out.println(s);
    }
}

From source file:Main.java

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

    syncList.add("one");
    syncList.add("two");
    syncList.add("three");

    synchronized (syncList) {
        Iterator<String> iterator = syncList.iterator();
        while (iterator.hasNext()) {
            System.out.println("item: " + iterator.next());
        }//w  ww. java  2  s  . c o  m
    }
}