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[]) {
    // create an array of string objs
    String init[] = { "One", "Two", "Three", "One", "Two", "Three", "from java2s.com" };

    // create one list
    List<String> list = new ArrayList<String>(Arrays.asList(init));

    System.out.println("List value before: " + list);

    // create singleton list
    list = Collections.singletonList("One");

    System.out.println("List value after: " + list);
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    List stuff = Arrays.asList(new String[] { "a", "b" });

    List list = new ArrayList(stuff);
    list = Collections.unmodifiableList(list);

    try {//w ww  .j  a v  a  2  s  .  c o  m
        list.set(0, "new value");
    } catch (UnsupportedOperationException e) {
        System.out.println(e.getMessage());
    }

}

From source file:Main.java

public static void main(String[] args) {
    List<String> yearList = new ArrayList<>(25);
    yearList.add("042015");
    yearList.add("052015");
    yearList.add("062015");
    yearList.add("072015");
    yearList.add("082015");
    yearList.add("092015");
    yearList.add("102010");
    yearList.add("112010");
    yearList.add("122010");
    yearList.add("012015");
    yearList.add("022015");
    yearList.add("032015");

    Collections.sort(yearList, new Comparator<String>() {
        private DateFormat format = new SimpleDateFormat("MMyyyy");

        @Override//from  ww  w.ja va2  s . c o  m
        public int compare(String o1, String o2) {
            int result = 0;
            try {
                Date d1 = format.parse(o1);
                try {
                    Date d2 = format.parse(o2);
                    result = d1.compareTo(d2);
                } catch (ParseException ex) {
                    result = -1;
                }
            } catch (ParseException ex) {
                result = 1;
            }
            return result;
        }
    });
    System.out.println(yearList);
}

From source file:MainClass.java

public static void main(String args[]) {
    String orig[] = { "1st", "2nd", "3rd", "4th", "5th", "1st", "2nd", "3rd", "4th", "5th" };
    String act[] = { "2nd", "3rd", "6th" };
    List origList = new ArrayList(Arrays.asList(orig));
    List actList = Arrays.asList(act);

    System.out.println(origList.retainAll(actList));
    System.out.println(origList);
}

From source file:Main.java

public static void main(String[] args) {
    Random random = new Random();
    Set<Integer> set = new HashSet<Integer>();
    while (set.size() < 5) {
        set.add(random.nextInt());//from  w ww  .j ava  2 s.  com
    }
    List<Integer> result = new ArrayList<Integer>(set);
    System.out.println(result);
}

From source file:Main.java

public static void main(String[] args) {
    Set<String> allZones = ZoneId.getAvailableZoneIds();
    List<String> zoneList = new ArrayList<String>(allZones);
    Collections.sort(zoneList);/*from   w  ww.j av  a 2  s.co  m*/

    LocalDateTime dt = LocalDateTime.now();
    for (String s : zoneList) {
        ZoneId zone = ZoneId.of(s);
        ZonedDateTime zdt = dt.atZone(zone);
        ZoneOffset offset = zdt.getOffset();
        String out = String.format("%35s %10s%n", zone, offset);
        System.out.println(out);
    }
}

From source file:Main.java

public static void main(String[] argv) throws Exception {

    List stuff = Arrays.asList(new String[] { "a", "b" });

    List list = new ArrayList(stuff);
    list = Collections.unmodifiableList(list);

    try {/*from   w  ww  .  ja v a 2 s  .  c o m*/
        list.set(0, "new value");
    } catch (UnsupportedOperationException e) {

    }

    Set set = new HashSet(stuff);
    set = Collections.unmodifiableSet(set);

    Map map = new HashMap();
    map = Collections.unmodifiableMap(map);
}

From source file:MainClass.java

public static void main(String args[]) {
    String simpsons[] = { "B", "H", "L", "M", "H", "M", "R" };

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

    // Ensure list sorted
    Collections.sort(list);// w ww. ja va  2  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);

}

From source file:Main.java

public static void main(String[] argv) throws Exception {
    List stuff = Arrays.asList(new String[] { "a", "b" });

    List list = new ArrayList(stuff);
    List list2 = new LinkedList(list);

    Set set = new HashSet(stuff);
    Set set2 = new TreeSet(set);

    Map map = new HashMap();
    Map map2 = new TreeMap(map);
}

From source file:Main.java

public static void main(String args[]) {

    Hashtable<String, String> h = new Hashtable<String, String>();
    h.put("a", "b");
    h.put("c", "d");
    h.put("e", "f");

    for (String str : h.keySet()) {
        System.out.println(str);/*from w ww.ja v  a  2  s  . c  om*/
    }
    List<String> v = new ArrayList<String>(h.keySet());
    Collections.sort(v);

    for (String str : v) {
        System.out.println(str + " " + (String) h.get(str));
    }
}