Example usage for java.util List toArray

List of usage examples for java.util List toArray

Introduction

In this page you can find the example usage for java.util List toArray.

Prototype

Object[] toArray();

Source Link

Document

Returns an array containing all of the elements in this list in proper sequence (from first to last element).

Usage

From source file:Main.java

public static void main(String[] argv) throws Exception {
    List list = new ArrayList();

    Object[] objectArray = list.toArray();
    MyClass[] array = (MyClass[]) list.toArray(new MyClass[list.size()]);

}

From source file:Main.java

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

    List<String> l = (Arrays.asList());
    String stuff[] = (String[]) l.toArray();
}

From source file:MainClass.java

public static void main(String args[]) throws Exception {
    CertificateFactory cf = CertificateFactory.getInstance("X.509");
    List mylist = new ArrayList();
    for (int i = 0; i < args.length; i++) {
        FileInputStream in = new FileInputStream(args[i]);
        Certificate c = cf.generateCertificate(in);
        mylist.add(c);//from  w ww  .  j  a v  a  2  s.  c  o  m
    }
    CertPath cp = cf.generateCertPath(mylist);
    List cplist = cp.getCertificates();
    Object[] o = cplist.toArray();
    for (int i = 0; i < o.length; i++) {
        X509Certificate c = (X509Certificate) o[i];
        System.out.println(c.getSubjectDN());
        byte[] pbk = c.getPublicKey().getEncoded();
        for (int j = 0; j < pbk.length; j++) {
            System.out.print(pbk[j] + ",");
        }
        System.out.println("\nIssued by " + c.getIssuerDN());
    }
}

From source file:Main.java

public static void main(String[] args) {
    Set<Object> set = new HashSet<Object>();
    set.add("A");
    set.add(new Long(10));
    set.add(new Date());

    List<Object> list = new ArrayList<Object>(set);
    Object[] objects = list.toArray();

    for (int i = 0; i < objects.length; i++) {
        Object object = objects[i];
        System.out.println("Object = " + object);
    }/*from ww  w. j  a  va2 s.  c om*/
}

From source file:MainClass.java

public static void main(String[] a) {
    List list = Arrays.asList(new String[] { "A", "B", "C", "D" });

    Object[] objArray = list.toArray();

    for (Object obj : objArray) {
        System.out.println(obj);/*from   w  ww  .  j av a 2 s .  c  o  m*/
    }

    String[] strArray = (String[]) list.toArray(new String[list.size()]);

    for (String string : strArray) {
        System.out.println(string);
    }
}

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();
    System.out.println(Arrays.toString(c));
}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Dish> menu = Arrays.asList(new Dish("pork", false, 800, Dish.Type.MEAT),
            new Dish("beef", false, 700, Dish.Type.MEAT), new Dish("chicken", false, 400, Dish.Type.MEAT),
            new Dish("rice", true, 350, Dish.Type.OTHER), new Dish("pizza", true, 550, Dish.Type.OTHER),
            new Dish("prawns", false, 400, Dish.Type.FISH), new Dish("salmon", false, 450, Dish.Type.FISH));
    List<Integer> numbers = Arrays.asList(3, 4, 5, 1, 2);

    Arrays.stream(numbers.toArray()).forEach(System.out::println);
    int calories = menu.stream().mapToInt(Dish::getCalories).sum();
    System.out.println("Number of calories:" + calories);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Dish> menu = Arrays.asList(new Dish("pork", false, 800, Dish.Type.MEAT),
            new Dish("beef", false, 700, Dish.Type.MEAT), new Dish("chicken", false, 400, Dish.Type.MEAT),
            new Dish("rice", true, 350, Dish.Type.OTHER), new Dish("pizza", true, 550, Dish.Type.OTHER),
            new Dish("prawns", false, 400, Dish.Type.FISH), new Dish("salmon", false, 450, Dish.Type.FISH));
    List<Integer> numbers = Arrays.asList(3, 4, 5, 1, 2);

    Arrays.stream(numbers.toArray()).forEach(System.out::println);
    // max and OptionalInt
    OptionalInt maxCalories = menu.stream().mapToInt(Dish::getCalories).max();
    System.out.println("Number of calories:" + maxCalories);

}

From source file:Main.java

public static void main(String[] args) throws Exception {
    List<Dish> menu = Arrays.asList(new Dish("pork", false, 800, Dish.Type.MEAT),
            new Dish("beef", false, 700, Dish.Type.MEAT), new Dish("chicken", false, 400, Dish.Type.MEAT),
            new Dish("rice", true, 350, Dish.Type.OTHER), new Dish("pizza", true, 550, Dish.Type.OTHER),
            new Dish("prawns", false, 400, Dish.Type.FISH), new Dish("salmon", false, 450, Dish.Type.FISH));
    List<Integer> numbers = Arrays.asList(3, 4, 5, 1, 2);

    Arrays.stream(numbers.toArray()).forEach(System.out::println);
    // max and OptionalInt
    OptionalInt maxCalories = menu.stream().mapToInt(Dish::getCalories).max();
    System.out.println("Number of calories:" + maxCalories);

    int max;/*  w w  w  . j a  v  a2s. co  m*/
    if (maxCalories.isPresent()) {
        max = maxCalories.getAsInt();
    } else {
        // we can choose a default value
        max = 1;
    }
    System.out.println(max);
}

From source file:Main.java

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

    for (int i = 0; i < 25; i++) {
        numbers.add(i);//from   ww  w.  j a  v  a  2s. c  o m
    }

    System.out.println(Arrays.toString(numbers.toArray()));

    Collections.rotate(numbers, 10);

    System.out.println(Arrays.toString(numbers.toArray()));
}