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

    Vector<String> v = new Vector<String>();
    v.add("4");/*  w ww .jav a  2s  . c  o  m*/
    v.add("5");

    // insert all elements of Vector to ArrayList at index 1
    arrayList.addAll(1, v);

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

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

    // Get an object of ListIterator using listIterator() method

    ListIterator listIterator = aList.listIterator();
    listIterator.next();//from  w ww .  j a v  a  2 s. com
    listIterator.next();

    // remove element returned by last next method

    listIterator.remove();
    for (String str : aList) {
        System.out.println(str);
    }
}

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

    List lst = arrayList.subList(1, 3);
    for (int i = 0; i < lst.size(); i++)
        System.out.println(lst.get(i));

    // remove one element from sub list
    Object obj = lst.remove(0);/*from  w w  w. ja va  2 s .  c  o  m*/
    System.out.println(obj + " is removed");

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

From source file:Main.java

public static void main(String[] args) {
    ArrayList<String> al = new ArrayList<String>();
    al.add("Java");
    al.add("SQL");
    al.add("Data");

    System.out.println("ArrayList:" + al);
    String[] s1 = new String[al.size()];

    String[] s2 = al.toArray(s1);

    System.out.println("s1 == s2:" + (s1 == s2));
    System.out.println("s1:" + Arrays.toString(s1));
    System.out.println("s2:" + Arrays.toString(s2));

    s1 = new String[1];
    s1[0] = "hello"; // Store hello in first element

    s2 = al.toArray(s1);//from w w w .  j  ava2s. c  om

    System.out.println("s1 == s2:" + (s1 == s2));
    System.out.println("s1:" + Arrays.toString(s1));
    System.out.println("s2:" + Arrays.toString(s2));
}

From source file:Main.java

public static void main(String[] a) {
    ArrayList<String> nums = new ArrayList<String>();
    nums.clear();//from   w  w w.j  a v  a 2 s.  c  om
    nums.add("One");
    nums.add("Two");
    nums.add("Three");

    System.out.println(nums);
    nums.set(0, "Uno");
    nums.set(1, "Dos");
    nums.set(2, "Tres");
    System.out.println(nums);
}

From source file:Main.java

public static void main(String[] args) {
    Vector<String> v = new Vector<String>();
    v.add("1");/*from www  .  j  a v a2  s  . co m*/
    v.add("2");
    v.add("3");

    ArrayList<String> arrayList = new ArrayList<String>();
    arrayList.add("One");
    arrayList.add("Two");
    arrayList.add("Three");
    arrayList.add("Four");
    arrayList.add("Five");

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

}

From source file:Main.java

public static void main(String[] a) {
    ArrayList<String> nums = new ArrayList<String>();
    nums.clear();//from   w  w  w. j  a  v a 2  s. c o  m
    nums.add("One");
    nums.add("Two");
    nums.add("Three");

    System.out.println(nums);
    nums.set(0, "Uno");
    nums.set(1, "Dos");
    nums.set(2, "java2s.com");
    System.out.println(nums);
}

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

    Vector<String> v = new Vector<String>();
    v.add("4");//from w w w.  java2 s  .  c om
    v.add("5");

    // append all elements of Vector to ArrayList
    arrayList.addAll(v);

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

}

From source file:MainClass.java

public static void main(String args[]) {

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

    al.add(1);
    al.add(2);//  w  w w.  j av a 2 s  .  c o  m
    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<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  w w. j  a  va 2  s.  c  om

    // Removes first occurrence of "E"
    boolean value = arrlist.remove("E");

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

    System.out.println("Value = " + arrlist);
}