Example usage for java.util ArrayList add

List of usage examples for java.util ArrayList add

Introduction

In this page you can find the example usage for java.util ArrayList add.

Prototype

public void add(int index, E element) 

Source Link

Document

Inserts the specified element at the specified position in this list.

Usage

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  w  ww.  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:Main.java

public static java.util.List<String> getLabelsFromInt(Cursor c) {
    c.moveToFirst();/*from  www  .  j a  v a  2  s.co m*/
    ArrayList<String> res = new ArrayList<>();
    for (int i = 0; i < c.getCount(); i++) {
        res.add(i, String.valueOf(c.getInt(0)));
        c.moveToNext();
    }
    return res;
}

From source file:Main.java

public static ArrayList<Byte> toByteArray(byte[] data) {
    ArrayList<Byte> rval = new ArrayList<>(data.length);
    for (int i = 0; i < data.length; i++) {
        rval.add(i, new Byte(data[i]));
    }/*from w w  w  .java 2  s .co  m*/
    return rval;
}