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(Collection<? extends E> c) 

Source Link

Document

Constructs a list containing the elements of the specified collection, in the order they are returned by the collection's iterator.

Usage

From source file:Main.java

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

    // use add() method to add elements in the list
    arrlist.add(20);/*from  www.  j av  a 2 s  . c  om*/
    arrlist.add(30);
    arrlist.add(10);
    arrlist.add(50);

    System.out.println(arrlist);

    // finding size of this list
    int retval = arrlist.size();
    System.out.println("List consists of " + retval + " elements");

    arrlist.clear();
    retval = arrlist.size();
    System.out.println("list consists of " + retval + " elements");
}

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  ww w  .  ja va  2s. com*/

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

}

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 www  .j a  v  a2 s  .co  m*/

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

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

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

From source file:Main.java

public static void main(String args[]) {

    ArrayList<String> arrList = new ArrayList<>(Arrays.asList("a", "a", "a", "from java2s.com"));

    // Trim the arraylist
    arrList.trimToSize();/*from ww w.j a va  2  s. c om*/

    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);/*from   ww w .  j a  va2s.c o m*/
    arrlist.add(2);
    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);//from   w ww. j a  v a2  s. com
    arrlist.add(2);
    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<Integer> arrlist = new ArrayList<Integer>(5);

    arrlist.add(1);/*from  ww w  . j  a  v  a  2 s.c  o  m*/
    arrlist.add(2);
    arrlist.add(3);
    arrlist.add(4);
    arrlist.add(5);

    System.out.println(arrlist);

    List<Integer> arrlist2 = arrlist.subList(2, 4);

    System.out.println(arrlist2);

}

From source file:MainClass.java

public static void main(String[] a) {
    List list = new ArrayList(10);
    Object anObject = "A";
    list.add("N");
    Collections.fill(list, anObject);
    System.out.println(list);/*from  w  w  w  .ja  v  a 2  s  .  c om*/
    System.out.println(list.size());
}

From source file:Main.java

public static void main(String[] args) {

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

    arrlist.add(20);/*w  w  w  . ja v  a  2 s  .  c  om*/
    arrlist.add(40);
    arrlist.add(10);
    arrlist.add(15);
    arrlist.add(25);

    Object[] ob = arrlist.toArray();

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

}

From source file:Main.java

public static void main(String[] args) {
    List<String> data = new ArrayList<String>(Arrays.asList(new String[] { "a", "b", "c", "d", "f", "g" }));
    System.out.println(data);//from   w w w .  j a  va2s  .  c o  m
    removeWithoutUsingRemoveMethod(data, 2);
    System.out.println(data);
}