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) {

    List<String> carList = new ArrayList<String>();

    carList.add("A");
    carList.add("B");
    carList.add("C");
    carList.add("D");

    String[] carArray = carList.toArray(new String[0]);

    for (String car : carArray) {
        System.out.println(car);//from   w w  w.  ja va 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("C");
    arrayList.add("D");
    arrayList.add("E");

    System.out.println(arrayList);
    Collections.reverse(arrayList);
    System.out.println(arrayList);
}

From source file:Main.java

public static void main(String args[]) {
    List<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");

    for (String s : list) {
        if (s.equals("B")) {
            list.remove("B");
        }/*  w  w w.  ja va 2s  .c o  m*/
        System.out.println(s);
    }
}

From source file:Main.java

public static void main(String[] args) {
    ArrayList aList = new ArrayList();
    aList.add("1");
    aList.add("2");
    aList.add("3");
    aList.add("4");
    aList.add("java2 s .com");
    System.out.println("ArrayList: ");
    System.out.println(aList);/*from   w w w.ja  v  a 2 s. c  o m*/
    Iterator itr = aList.iterator();
    String strElement = "";
    while (itr.hasNext()) {
        strElement = (String) itr.next();
        if (strElement.equals("2")) {
            itr.remove();
            break;
        }
    }
    System.out.println("ArrayList after removal : ");
    System.out.println(aList);
}

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("java2s.com");

    ListIterator<String> listIterator = aList.listIterator();
    listIterator.next();//from  ww w  . j a v  a2  s .  c o  m
    listIterator.add("Added Element");
    for (String str : aList) {
        System.out.println(str);
    }
}

From source file:Main.java

public static void main(String[] args) {

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

    arrlist.add(20);/*from  ww  w.j  a  v a  2  s.  c  o  m*/
    arrlist.add(25);
    arrlist.add(10);
    arrlist.add(15);

    System.out.println(arrlist);

    // list contains element 10
    boolean retval = arrlist.contains(10);

    if (retval == true) {
        System.out.println("element 10 is contained in the list");
    } else {
        System.out.println("element 10 is not contained in the list");
    }

    // list does not contain element 30
    boolean retval2 = arrlist.contains(30);

    if (retval2 == true) {
        System.out.println("element 30 is contained in the list");
    } else {
        System.out.println("element 30 is not contained in the list");
    }
}

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

    System.out.println(arrayList);
    Collections.fill(arrayList, "REPLACED");
    System.out.println(arrayList);

}

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();
    listIterator.next();//from   w w w. jav a2  s .  c om
    listIterator.add("Added Element");
    for (String str : aList) {
        System.out.println(str);
    }
}

From source file:Main.java

public static void main(String[] args) {

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

    arrlist.add(10);// w w  w.  ja  va2s.  c om
    arrlist.add(12);
    arrlist.add(31);
    arrlist.add(49);

    // toArray copies content into other array
    Integer myArray[] = new Integer[arrlist.size()];
    myArray = arrlist.toArray(myArray);

    System.out.println(Arrays.toString(myArray));

}

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  w w.ja va2 s.  c o  m*/
    System.out.println("Sorted ArrayList contains : " + arrayList);
    int index = Collections.binarySearch(arrayList, "4");
    System.out.println("Element found at : " + index);
}