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) {
    // setup/*from  w  w  w . j a  v  a  2  s  . c  om*/
    List<Integer> list = new ArrayList<Integer>();
    list.add(4);
    list.add(3);

    // classic way
    list.sort(new Comparator<Integer>() {

        @Override
        public int compare(Integer a, Integer b) {
            return a - b;
        }
    });
    System.out.println(list);

    // lambda way, the shortest way
    list.sort((a, b) -> a - b);
    System.out.println(list);

}

From source file:MainClass.java

public static void main(String args[]) {

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

    al.add(1);/*from w w  w .  j  a v  a2 s . c o m*/
    al.add(2);
    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) {
    Collection<Integer> col = new ArrayList<Integer>();
    col.add(0);//  ww w.ja v a 2  s  .  co m
    ArrayDeque<Integer> deque = new ArrayDeque<Integer>(col);

    deque.add(3);
    deque.add(18);
    deque.add(25);
    deque.add(18);

    System.out.println(deque);

}

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");//  ww  w  .  ja v  a  2s .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:Main.java

public static void main(String[] args) {
    // setup//from  w ww .j a  v  a 2 s. co  m
    List<Integer> list = new ArrayList<Integer>();
    list.add(4);
    list.add(3);

    // classic way
    list.sort(new Comparator<Integer>() {

        @Override
        public int compare(Integer a, Integer b) {
            return a - b;
        }
    });
    System.out.println(list);

    // lambda way
    Comparator<Integer> lambdaComparator = (Integer a, Integer b) -> b - a;
    list.sort(lambdaComparator);
    System.out.println(list);

}

From source file:Main.java

public static void main(String[] args) {
    // create array list
    List<Character> list = new ArrayList<Character>();

    // populate the list
    list.add('X');
    list.add('Y');

    System.out.println("Initial list: " + list);

    // make the list unmodifiable
    List<Character> immutablelist = Collections.unmodifiableList(list);

    // try to modify the list
    immutablelist.add('Z');
}

From source file:Main.java

public static void main(String[] args) {
    System.out.println(new Vector().getClass().getPackage().getName());
    System.out.println(new ArrayList().getClass().getPackage().getName());
    System.out.println("Test String".getClass().getPackage().getName());
    System.out.println(new Integer(1).getClass().getPackage().getName());
}

From source file:Main.java

public static void main(String[] args) {
    // Create a list of strings
    List<String> names = new ArrayList<>();
    names.add("A");
    names.add("B");
    names.add("C");
    // Get an iterator for the list
    Iterator<String> nameIterator = names.iterator();
    // Iterate over all elements in the list
    while (nameIterator.hasNext()) {
        // Get the next element from the list
        String name = nameIterator.next();
        System.out.println(name);
    }/*www.  ja v a2  s  .c  om*/

}

From source file:Main.java

public static void main(String[] args) {
    // Create a list of strings
    List<String> names = new ArrayList<>();
    names.add("A");
    names.add("B");
    names.add("C");

    Iterator<String> nameIterator = names.iterator();
    // Iterate over all elements in the list
    while (nameIterator.hasNext()) {
        // Get the next element from the list
        String name = nameIterator.next();
        System.out.println(name);
        nameIterator.remove();/*  w w  w  .j a  v  a 2 s  .co m*/
    }
    System.out.println(names);
}

From source file:Main.java

public static void main(String[] args) {
    List<String> list = new ArrayList<>();
    list.add("acowa");
    list.add("cow");
    list.add("aow");
    Collections.sort(list, new Comparator<String>() {

        @Override//from w w  w . ja  v  a  2s .  c  o m
        public int compare(String o1, String o2) {
            if (o1.length() > o2.length()) {
                return 1;
            } else {
                return o1.compareTo(o2);
            }
        }
    });

    System.out.println(list);
}