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

public static void main(String args[]) {
    int i = 35;/*  w  w w  . j a v  a  2  s .  c  o  m*/
    Validate.isTrue(i > 30); // Passes ok
    // Validate.isTrue(i > 40, "Invalid value: ", i); // throws custom Exception

    List data = new ArrayList();
    // Validate.notEmpty(data, "Collection cannot be empty"); // throws custom Exception

    data.add(null);
    Validate.noNullElements(data, "Collection contains null elements"); // throws custom Exception

}

From source file:Capture.java

public static void main(String[] args) {
    List<?> list = new ArrayList<Date>();
    Set<?> set = listToSet(list);
}

From source file:Main.java

public static void main(String[] args) {
    List ls = new ArrayList();
    ls.add(1);/*from w  w w  .j  a v a  2  s  . c  o m*/
    ls.add(1);
    ls.add(2);
    ls.add(5);
    ls.add(4);
    ls.add(4);

    TreeSet s1 = copySet(ls);
    System.out.println(ls);
    System.out.println(s1);

}

From source file:Main.java

public static void main(String[] argv) {
    JTextPane textPane = new JTextPane();

    List list = new ArrayList();

    TabStop[] tstops = (TabStop[]) list.toArray(new TabStop[0]);
    TabSet tabs = new TabSet(tstops);

    Style style = textPane.getLogicalStyle();
    StyleConstants.setTabSet(style, tabs);
    textPane.setLogicalStyle(style);/*w  ww.  j av  a2  s  .  co m*/
}

From source file:Main.java

public static void main(String[] args) {
    List<Phone> phones = new ArrayList<>();
    phones.add(new Phone("Galaxy", 12345));
    phones.add(new Phone("iPhone", 12345));

    JPanel panel = new JPanel(new GridLayout(0, 1));
    for (Phone phone : phones) {
        String html = "<html><body style='width:100px'>Phone: " + phone.getName() + "<br/>Model: "
                + phone.getModel() + "</body></html>";
        JLabel label = new JLabel(html);
        label.setBorder(new MatteBorder(0, 0, 1, 0, Color.BLACK));
        panel.add(label);/*from   w w  w  . j av a2 s .  c  om*/

    }
    JOptionPane.showMessageDialog(null, panel, "Phone List", JOptionPane.PLAIN_MESSAGE);
}

From source file:MovingPlanets.java

public static void main(String args[]) {
    String names[] = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto" };
    List planets = new ArrayList();
    for (int i = 0, n = names.length; i < n; i++) {
        planets.add(names[i]);//from w w w . jav  a 2s.co m
    }
    ListIterator lit = planets.listIterator();
    String s;
    lit.next();
    lit.next();
    s = (String) lit.next();
    lit.remove();
    lit.next();
    lit.next();
    lit.next();
    lit.add(s);
    lit.next(); // Gets back just added
    lit.previous();
    lit.previous();
    s = (String) lit.previous();
    lit.remove();
    lit.next();
    lit.next();
    lit.add(s);
    Iterator it = planets.iterator();
    while (it.hasNext()) {
        System.out.println(it.next());
    }
}

From source file:Main.java

public static void main(String[] argv) {
    DayOfWeek d = DayOfWeek.FRIDAY;
    List<DayOfWeek> l = new ArrayList<>();
    l.add(d);//from   w  ww .j  av  a 2 s. c  o m

    System.out.println(getNextClosestDateTime(l, 22, 20));
}

From source file:Main.java

public static void main(String... args) {
    final int MAX_VAL = 99999;
    List<Integer> linkedList = new LinkedList<Integer>();
    List<Integer> arrayList = new ArrayList<Integer>();
    for (int i = 0; i < MAX_VAL; i++) {
        linkedList.add(i);/*from w w  w . jav  a  2 s  .co m*/
        arrayList.add(i);
    }
    long time = System.nanoTime();

    for (int i = 0; i < MAX_VAL; i++) {
        linkedList.add(MAX_VAL / 2, i);
    }
    System.out.println("LinkedList add:" + (System.nanoTime() - time));

    time = System.nanoTime();
    for (int i = 0; i < MAX_VAL; i++) {
        arrayList.add(MAX_VAL / 2, i);
    }
    System.out.println("ArrayList add:" + (System.nanoTime() - time));

    // Reset the lists
    linkedList = new LinkedList<Integer>();
    arrayList = new ArrayList<Integer>();
    for (int i = 0; i < MAX_VAL; i++) {
        linkedList.add(i);
        arrayList.add(i);
    }

    time = System.nanoTime();
    ListIterator<Integer> li = linkedList.listIterator(MAX_VAL / 2);
    for (int i = 0; i < MAX_VAL; i++) {
        li.add(i);
    }
    System.out.println("LinkedList iterator add:" + (System.nanoTime() - time));

    time = System.nanoTime();
    ListIterator<Integer> ali = arrayList.listIterator(MAX_VAL / 2);
    for (int i = 0; i < MAX_VAL; i++) {
        ali.add(i);
    }
    System.out.println("ArrayList iterator add:" + (System.nanoTime() - time));
}

From source file:Main.java

public static void main(String[] args) {
    Person p = new Person("A");
    Animal a = new Animal("B");
    Thing t = new Thing("C");
    String text = "hello";
    Integer number = 1000;/*from   www. ja v a  2  s .  c  o  m*/

    List<Object> list = new ArrayList<Object>();
    list.add(p);
    list.add(a);
    list.add(t);
    list.add(text);
    list.add(number);

    for (int i = 0; i < list.size(); i++) {
        Object o = list.get(i);
        if (o instanceof Person) {
            System.out.println("My name is " + ((Person) o).getName());
        } else if (o instanceof Animal) {
            System.out.println("I live in " + ((Animal) o).getHabitat());
        } else if (o instanceof Thing) {
            System.out.println("My color is " + ((Thing) o).getColor());
        } else if (o instanceof String) {
            System.out.println("My text is " + o.toString());
        } else if (o instanceof Integer) {
            System.out.println("My value is " + ((Integer) o));
        }
    }
}

From source file:MyComparator.java

public static void main(String args[]) {
    List<String> ts = new ArrayList<String>();

    ts.add("tutorial");
    ts.add("from");
    ts.add("java2s.com");

    Collections.sort(ts);//from   w w w. j a v  a 2s. c  om
    for (String element : ts) {
        System.out.println(element + " ");
    }
    System.out.println();
    Collections.sort(ts, new MyComparator());
    for (String element : ts) {
        System.out.println(element + " ");
    }

}