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

public static void main(String args[]) {
    List list = new ArrayList(Arrays.asList("a", "b", "c"));
    Iterator i = list.iterator();
    while (i.hasNext()) {
        System.out.println(i.next());
        list.add("Add");
    }/*from   w  w  w.  j  a va 2 s  .c  o  m*/
}

From source file:Main.java

public static void main(String args[]) {

    List<String> srclst = new ArrayList<String>(5);
    List<String> destlst = new ArrayList<String>(10);

    // populate two lists
    srclst.add("tutorial");
    srclst.add("from");
    srclst.add("java2s.com");

    destlst.add("Java Tutorial");
    destlst.add("From");
    destlst.add("Java2s.com");

    // copy into dest list
    Collections.copy(destlst, srclst);

    System.out.println("Value of source list: " + srclst);
    System.out.println("Value of destination list: " + destlst);
}

From source file:Main.java

public static void main(String[] args) {
    Vector vec = new Vector(8);
    List sublist = new ArrayList(10);

    vec.add(4);/*www.  ja v  a  2 s.  c om*/
    vec.add(3);
    vec.add(2);
    vec.add(1);
    vec.add(6);
    vec.add(7);
    vec.add(9);
    vec.add(5);

    sublist = vec.subList(2, 6);

    System.out.println(sublist);
}

From source file:Main.java

public static void main(String args[]) {
    List<Integer> list = Arrays.asList(1, 2, 3);
    ArrayList<Integer> val = new ArrayList<Integer>(list);
    System.out.println(val.size());
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    String elements[] = { "A", "B", "C", "D" };

    List list = new ArrayList(Arrays.asList(elements));

    System.out.println(list);/*  w w w .j  a  va  2 s.c  o  m*/
}

From source file:Main.java

public static void main(String args[]) {
    String a[] = { "a", "b", "c" };
    String b[] = { "d", "e" };

    List<String> list = new ArrayList<String>(Arrays.asList(a));
    list.addAll(Arrays.asList(b));

    Object[] c = list.toArray();//from   ww w  .  j  a  va 2 s. c o  m
    System.out.println(Arrays.toString(c));
}

From source file:SearchTest.java

public static void main(String args[]) {
    String simpsons[] = { "Bart", "Hugo", "Lisa", "Marge", "Homer", "Maggie", "Roy" };

    // Convert to list
    ArrayList list = new ArrayList(Arrays.asList(simpsons));

    // Ensure list sorted
    Collections.sort(list);//from   www  . j  av  a 2 s .  c o  m
    System.out.println("Sorted list: [length: " + list.size() + "]");
    System.out.println(list);

    // Search for element in list
    int index = Collections.binarySearch(list, "Maggie");
    System.out.println("Found Maggie @ " + index);

    // Search for element not in list
    index = Collections.binarySearch(list, "Jimbo Jones");
    System.out.println("Didn't find Jimbo Jones @ " + index);

    // Insert
    int newIndex = -index - 1;
    list.add(newIndex, "Jimbo Jones");
    System.out.println("With Jimbo Jones added: [length: " + list.size() + "]");
    System.out.println(list);
}

From source file:MainClass.java

public static void main(String[] args) {
    Queue queue = new LinkedList();
    queue.add("Hello");
    queue.add("World");
    List list = new ArrayList(queue);

    System.out.println(list);//from w w  w. j a  v  a2s . co m
}

From source file:Main.java

public static void main(String[] args) {
    Integer[] intArray = { 1, 2, 3, 4, 5, 6, 7, 8 };
    List<Integer> listOfIntegers = new ArrayList<>(Arrays.asList(intArray));

    Comparator<Integer> normal = Integer::compare;
    Comparator<Integer> reversed = normal.reversed();
    Collections.sort(listOfIntegers, reversed);

    listOfIntegers.stream().forEach(e -> System.out.print(e + " "));
    System.out.println("");

}

From source file:MainClass.java

public static void main(String args[]) {
    String str[] = { "B", "H", "L", "M", "I", "N", "R" };
    // Convert to list
    List list = new ArrayList(Arrays.asList(str));
    // Ensure list sorted
    Collections.sort(list);/* w ww.j a  va2 s.  com*/
    System.out.println("Sorted list: [length: " + list.size() + "]");
    System.out.println(list);
    // Search for element in list
    int index = Collections.binarySearch(list, "M");
    System.out.println("Found M @ " + index);
    // Search for element not in list
    index = Collections.binarySearch(list, "J");
    System.out.println("Didn't find J @ " + index);
    // Insert
    int newIndex = -index - 1;
    list.add(newIndex, "J");
    System.out.println("With J added: [length: " + list.size() + "]");
    System.out.println(list);
}