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

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

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

    ArrayList<String> arrayList1 = new ArrayList<String>();
    arrayList1.add("1");
    arrayList1.add("2");
    arrayList1.add("3");

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

    arrayList2.add("One");
    arrayList2.add("Two");
    arrayList2.add("Three");
    arrayList2.add("Four");
    arrayList2.add("Five");

    System.out.println(arrayList2);

    Collections.copy(arrayList2, arrayList1);
    System.out.println(arrayList2);
}

From source file:Main.java

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

    list.add("A");
    list.add("B");
    list.add("C");
    list.add("C");
    list.add("C");
    list.add("C");
    list.add("C");
    list.add("C");

    for (Iterator<String> iter = list.iterator(); iter.hasNext();) {
        String s = iter.next();/*w w w .  ja va 2 s . co m*/
        if (s.equals("B")) {
            iter.remove();
        } else {
            System.out.println(s);
        }
    }

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

From source file:Main.java

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

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

    System.out.println(arrlist);

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

    System.out.println(arrlist2);

    // inserting all elements
    arrlist.addAll(arrlist2);

    System.out.println(arrlist);

}

From source file:Main.java

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

    arrlist.add(1);
    arrlist.add(2);//  ww  w  . ja  v a  2 s.co  m
    arrlist.add(4);

    System.out.println(arrlist);

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

    arrlist2.add(25);
    arrlist2.add(30);
    arrlist2.add(31);
    arrlist2.add(35);

    System.out.println(arrlist2);

    // inserting all elements of list2 at third position
    arrlist.addAll(2, arrlist2);

    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.add("4");
    arrayList.add("5");

    Vector<String> v = new Vector<String>();

    v.add("A");//www. ja  v  a2 s. c  o m
    v.add("B");
    v.add("D");
    v.add("E");
    v.add("F");
    v.add("G");
    v.add("H");

    System.out.println(v);
    Collections.copy(v, arrayList);
    System.out.println(v);
}

From source file:NewStyle.java

public static void main(String args[]) {

    // Now, list holds references of type String. 
    ArrayList<String> list = new ArrayList<String>();

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

    // Notice that Iterator is also generic. 
    Iterator<String> itr = list.iterator();

    // The following statement will now cause a compile-time eror. 
    //    Iterator<Integer> itr = list.iterator(); // Error! 

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

        // Now, the following line is a compile-time, 
        // rather than runtime, error. 
        //    Integer i = itr.next(); // this won't compile 

        System.out.println(str + " is " + str.length() + " chars long.");
    }/*ww  w. j a  v  a 2  s.  c om*/
}

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

    ListIterator itr = arrayList.listIterator();
    System.out.println("in forward direction");
    while (itr.hasNext()) {
        System.out.println(itr.next());
    }// w w  w  .j av  a2 s .  com

    System.out.println("in backward direction");
    while (itr.hasPrevious()) {
        System.out.println(itr.previous());
    }
}

From source file:Main.java

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

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

    System.out.println(arrayList.contains("2"));

    int index = arrayList.indexOf("4");
    if (index == -1)
        System.out.println("not contain 4");
    else//from w w  w. ja  v a2 s  .  co m
        System.out.println("4 at index :" + index);

    int lastIndex = arrayList.lastIndexOf("1");
    if (lastIndex == -1)
        System.out.println("not contain 1");
    else
        System.out.println("Last index :" + lastIndex);
}

From source file:ArrayListGenericDemo.java

public static void main(String[] args) {
    ArrayList<String> data = new ArrayList<String>();
    data.add("hello");
    data.add("goodbye");

    // data.add(new Date()); This won't compile!

    Iterator<String> it = data.iterator();
    while (it.hasNext()) {
        String s = it.next();//from   w  ww. j  av  a2  s .  com
        System.out.println(s);
    }
}