Example usage for java.util ArrayList add

List of usage examples for java.util ArrayList add

Introduction

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

Prototype

public boolean add(E e) 

Source Link

Document

Appends the specified element to the end of this list.

Usage

From source file:ArrayListToArray.java

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

    al.add(1);
    al.add(2);/*  w  w  w  .  j  a  va  2 s.c  om*/
    al.add(3);
    al.add(4);

    System.out.println("Contents of al: " + al);

    Integer ia[] = new Integer[al.size()];
    ia = al.toArray(ia);

    int sum = 0;

    for (int i : ia)
        sum += i;

    System.out.println("Sum is: " + sum);
}

From source file:Main.java

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

    arrlist.add(1);
    arrlist.add(2);//from  w  w  w . j a  va 2 s  . c om
    arrlist.add(3);
    arrlist.add(4);
    arrlist.add(5);

    ListIterator<Integer> iterator = arrlist.listIterator(2);
    while (iterator.hasNext()) {
        Integer i = iterator.next();
        iterator.remove();

    }
    System.out.println(arrlist);

}

From source file:Main.java

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

    arrlist.add(1);
    arrlist.add(2);/* w w  w.  j a v  a2  s  .  co  m*/
    arrlist.add(3);
    arrlist.add(4);
    arrlist.add(5);

    ListIterator<Integer> iterator = arrlist.listIterator();
    while (iterator.hasNext()) {
        Integer i = iterator.next();
        iterator.remove();

    }
    System.out.println(arrlist);

}

From source file:Main.java

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

    arrayList.add("1");
    arrayList.add("3");
    arrayList.add("5");
    arrayList.add("2");
    arrayList.add("4");

    Collections.sort(arrayList);//w w  w  . j a  va2s .  co m

    for (String str : arrayList)
        System.out.println(str);
}

From source file:Main.java

public static void main(String[] args) {

    ArrayList<Integer> arrlist = new ArrayList<Integer>(5);

    arrlist.add(20);
    arrlist.add(15);/*from w w  w  .  ja v  a 2s  . c  om*/
    arrlist.add(30);
    arrlist.add(45);

    System.out.println("Size of list: " + arrlist.size());
    System.out.println(arrlist);

    arrlist.remove(2);

    System.out.println("Size of list: " + arrlist.size());
    System.out.println(arrlist);

}

From source file:Main.java

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

    arrlist.add(1);
    arrlist.add(2);//  w w w. ja v  a2  s  .co m
    arrlist.add(3);
    arrlist.add(4);
    arrlist.add(5);

    ArrayList<Integer> arrlist2 = new ArrayList<Integer>();
    arrlist2.add(1);
    arrlist2.add(2);
    arrlist2.add(3);

    arrlist.removeAll(arrlist2);

    System.out.println(arrlist);

}

From source file:Main.java

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

    arrlist.add(1);
    arrlist.add(2);//from ww  w.ja  va 2s .  c o m
    arrlist.add(3);
    arrlist.add(4);
    arrlist.add(5);

    ArrayList<Integer> arrlist2 = new ArrayList<Integer>();
    arrlist2.add(1);
    arrlist2.add(2);
    arrlist2.add(3);

    arrlist.retainAll(arrlist2);

    System.out.println(arrlist);

}

From source file:ArrayListExample.java

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

    al.add(new Integer(1));
    al.add(new Integer(2));
    al.add(new Integer(3));
    al.add(new Integer(4));
    al.add(new Integer(5));
    al.add(new Integer(6));
    al.add(new Integer(7));
    al.add(new Integer(8));
    al.add(new Integer(9));
    al.add(new Integer(10));

    al.set(5, new Integer(66));

    for (Iterator i = al.iterator(); i.hasNext();) {
        Integer integer = (Integer) i.next();
        System.out.println(integer);
    }/*from w ww.  j av a2 s.c o m*/
}

From source file:Main.java

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

    arrayList.add("A");
    arrayList.add("B");
    arrayList.add("C");
    arrayList.add("D");
    arrayList.add("E");

    System.out.println(arrayList);

    Collections.shuffle(arrayList);

    System.out.println(arrayList);

}

From source file:Main.java

public static void main(String[] args) {

    ArrayList<String> arrlist = new ArrayList<String>(5);

    arrlist.add("G");
    arrlist.add("E");
    arrlist.add("F");
    arrlist.add("M");
    arrlist.add("E");
    arrlist.add("from java2s.com");

    System.out.println("Size of list: " + arrlist.size());

    System.out.println(arrlist);/*from w  ww.  ja v a  2 s  .c o m*/

    // Retrieving the index of last occurrence of element "E"
    int retval = arrlist.lastIndexOf("E");
    System.out.println("The last occurrence of E is at index " + retval);

}