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[] argv) {
    List<DayOfWeek> list = new ArrayList<>();

    for (Month month : Month.values()) {
        DayOfWeek day = LocalDate.now().withYear(2010).with(month).with(TemporalAdjusters.lastDayOfMonth())
                .getDayOfWeek();//from  w  ww .j  av a2s  .co  m

        list.add(day);
    }

    System.out.println(list);
}

From source file:Main.java

public static void main(String[] argv) {
    // Create a text pane
    JTextPane textPane = new JTextPane();

    List list = new ArrayList();
    int pos = 400;
    int align = TabStop.ALIGN_DECIMAL;
    int leader = TabStop.LEAD_NONE;
    TabStop tstop = new TabStop(pos, align, leader);
    list.add(tstop);/* w w  w  . j a va2s  .c  o m*/
}

From source file:Main.java

public static void main(String[] args) throws java.lang.Exception {
    List<Person> people = new ArrayList<Person>();
    people.add(new Person("J", "S"));
    people.add(new Person("J", "S"));
    people.add(new Person("J", "F"));
    people.add(new Person("J", "W"));
    people.add(new Person("J", "B"));
    Set<Object> seen = new HashSet<Object>();
    for (Person p : people) {

        Wrap wrap = new Wrap(p);
        if (seen.add(wrap)) {
            System.out.println(p + " is new");
        } else {//ww  w .j a  v a 2 s  .c  om
            System.out.println(p + " is a duplicate");
        }
    }
}

From source file:Main.java

public static void main(String[] args) {
    Vector<String> v = new Vector<String>();
    v.add("1");/* w  w  w.  ja  v a 2 s .  co  m*/
    v.add("2");
    v.add("3");

    ArrayList<String> arrayList = new ArrayList<String>();
    arrayList.add("4");
    arrayList.add("5");

    v.addAll(arrayList);

    for (int i = 0; i < v.size(); i++) {
        System.out.println(v.get(i));
    }
}

From source file:PlanetSet.java

public static void main(String args[]) {
    String names[] = { "Mercury", "Venus", "Earth", "Mars", "Jupiter", "Saturn", "Uranus", "Neptune", "Pluto" };
    Collection planets = new ArrayList();
    for (int i = 0, n = names.length; i < n; i++) {
        planets.add(names[i]);//from   w  w w.  jav  a 2  s  .  c o m
    }
    String s[] = (String[]) planets.toArray(new String[0]);
    for (int i = 0, n = s.length; i < n; i++) {

        System.out.println(s[i]);
    }
    planets.remove(names[3]);
    System.out.println(names[1] + " " + planets.contains(names[1]));
    System.out.println(names[3] + " " + planets.contains(names[3]));
}

From source file:Main.java

public static void main(String[] args) {
    String givenstring = "this is a test this is a another test";
    String[] words = givenstring.split(" ");

    ArrayList<String> arr = new ArrayList<String>();
    for (int i = 0; i < words.length; i++) {
        arr.add(words[i]);/*from w w w. j av a  2 s. c o  m*/
    }
    while (arr.size() != 0) {
        String word = arr.get(0);
        int frequency = Collections.frequency(arr, word);
        arr.removeAll(Collections.singleton(word));
        System.out.println(word + frequency);
    }
}

From source file:Main.java

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

    ObservableList<String> observableList = FXCollections.observableList(list);
    observableList.addListener(new ListChangeListener() {
        @Override/*w  ww. j  a v  a 2 s .  c  om*/
        public void onChanged(ListChangeListener.Change change) {
            System.out.println("change!");
        }
    });
    observableList.add("item one");
    list.add("item two");
    System.out.println("Size: " + observableList.size());

}

From source file:Main.java

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

    ObservableList<String> observableList = FXCollections.observableList(list);
    observableList.addListener(new ListChangeListener() {

        @Override//from  ww w .  j  av  a  2 s . c  o  m
        public void onChanged(ListChangeListener.Change change) {
            System.out.println("Detected a change! ");
            while (change.next()) {
                System.out.println("Was added? " + change.wasAdded());
                System.out.println("Was removed? " + change.wasRemoved());
                System.out.println("Was replaced? " + change.wasReplaced());
                System.out.println("Was permutated? " + change.wasPermutated());
            }
        }
    });
    observableList.add("item one");
    list.add("item two");
    System.out.println("Size: " + observableList.size());

}

From source file:Main.java

public static void main(String[] args) {
    Vector<String> v = new Vector<String>();
    v.add("1");/*w w  w  .j av a 2  s  . c om*/
    v.add("2");
    v.add("3");

    ArrayList<String> arrayList = new ArrayList<String>();
    arrayList.add("One");
    arrayList.add("Two");
    arrayList.add("Three");
    arrayList.add("Four");
    arrayList.add("Five");

    System.out.println(arrayList);
    Collections.copy(arrayList, v);
    System.out.println(arrayList);

}

From source file:Main.java

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

    for (Integer i : Arrays.asList(0, 1, 2, 3, 4, 5, 6, 7))
        numbers.add(i);/*from w  ww  .  j  a v a 2  s .co  m*/
    printList(numbers); // 0,1,2,3,4,5,6,7

    // replaces each element with twice its value
    for (int index = 0; index < numbers.size(); index++) {
        numbers.set(index, numbers.get(index) * 2);
    }
    printList(numbers);

    // does nothing because list is not being changed
    for (Integer number : numbers) {
        number++;
    }
    printList(numbers);

    // same as above -- just different syntax
    for (Iterator<Integer> iter = numbers.iterator(); iter.hasNext();) {
        Integer number = iter.next();
        number++;
    }
    printList(numbers);

    for (ListIterator<Integer> iter = numbers.listIterator(); iter.hasNext();) {
        Integer number = iter.next();
        iter.add(number + 1);
    }
    printList(numbers);

    for (Iterator<Integer> iter = numbers.iterator(); iter.hasNext();) {
        Integer number = iter.next();
        if (number % 2 == 0) // if number is even
            iter.remove(); // remove it from the collection
    }
    printList(numbers); // 1,3,5,7,9,11,13,15

    // ListIterator<?> has a "set" method to replace elements
    for (ListIterator<Integer> iter = numbers.listIterator(); iter.hasNext();) {
        Integer number = iter.next();
        iter.set(number / 2); // divide each element by 2
    }
    printList(numbers); // 0,1,2,3,4,5,6,7

    // Use Java 8 Lambda
    List<Integer> list = numbers;
    list.stream().forEach(elem -> System.out.println("element " + elem));

}