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:Main.java

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

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

    Object[] objArray = arrayList.toArray();

    for (Object obj : objArray)
        System.out.println(obj);//from   w  w w.ja  v  a  2  s.  co  m
}

From source file:Main.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));

    System.out.println("contents of al : " + al);
    Object ia[] = al.toArray(); // get array
    int sum = 0;/*from  ww  w .j  a v a2s  .  c  o m*/

    for (int i = 0; i < ia.length; i++) {
        sum += ((Integer) ia[i]).intValue();
    }
    System.out.println("Sum is :" + sum);
}

From source file:Main.java

public static void main(String[] args) {

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

    arrlist.add(25);

    boolean retval = arrlist.isEmpty();
    if (retval == true) {
        System.out.println("list is empty");
    } else {//from w  w w.ja v a2  s.  co  m
        System.out.println("list is not empty");
    }

}

From source file:Main.java

public static void main(String[] args) {
    ArrayList<String> list = new ArrayList<>();
    list.add("A");
    list.add("B");
    list.add("C");
    list.add("D");
    list.add("E");
    list.add("F");
    list.add("G");
    list.add("H");

    System.out.println(list);//from   w ww. j av a2s . co m

    while (!list.isEmpty()) {
        long index = Math.round(Math.floor(Math.random() * list.size()));
        System.out.println("Name " + list.get((int) index));
        list.remove((int) index);
    }

}

From source file:Main.java

public static void main(String[] args) {

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

    arrlist.add(15);
    arrlist.add(22);//from ww w . j  ava2  s . c om
    arrlist.add(30);
    arrlist.add(40);

    // adding element 25 at third position
    arrlist.add(2, 25);

    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("2");
    arrayList.add("3");

    arrayList.set(1, "REPLACED ELEMENT");
    for (String str : arrayList) {
        System.out.println(str);/*from   w  w  w .  ja v  a  2  s.co  m*/
    }
}

From source file:Main.java

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

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

    Collections.sort(arrayList);//from  w  ww.ja  v a2  s .com
    System.out.println("Sorted ArrayList contains : " + arrayList);
    int index = Collections.binarySearch(arrayList, "4");
    System.out.println("Element found at : " + index);
}

From source file:Main.java

public static void main(String[] args) {
    ArrayList<String> aList = new ArrayList<String>();
    aList.add("1");
    aList.add("2");
    aList.add("3");
    aList.add("4");
    aList.add("5");

    ListIterator<String> listIterator = aList.listIterator();

    while (listIterator.hasNext()) {
        System.out.println(listIterator.next());
    }//from w  w w  .  j a v  a 2 s.c o  m
    while (listIterator.hasPrevious()) {
        System.out.println(listIterator.previous());
    }
}

From source file:NewStyle.java

public static void main(String args[]) {

    ArrayList<String> list = new ArrayList<String>();

    list.add("one");
    list.add("two");
    list.add("three");
    list.add("four");

    Iterator<String> itr = list.iterator();

    while (itr.hasNext()) {
        String str = itr.next(); // no cast needed

        System.out.println(str + " is " + str.length() + " chars long.");
    }/*w  ww.  jav a 2 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("D");
    arrayList.add("E");
    arrayList.add("F");

    Enumeration e = Collections.enumeration(arrayList);

    while (e.hasMoreElements())
        System.out.println(e.nextElement());
}