There is a way to get a List from varargs i.e. Arrays.asList(o1, o2, o3 etc)
Is there a similar way to get an array? E.g. something like Arrays.asArray(o1, o2, o3 etc)
Update:
o1,o2,o3 is different types of objects
There is a way to get a List from varargs i.e. Arrays.asList(o1, o2, o3 etc)
Is there a similar way to get an array? E.g. something like Arrays.asArray(o1, o2, o3 etc)
Update:
o1,o2,o3 is different types of objects
You can use constructions as String[] str = new String[] {"a", "b", "c"}
String[] str = {"a", "b", "c"};Use something like this:
Element[] array = new Element[] { new Element(1),new Element(2),new Element(3) }; Or you could just create a list and convert it to an array. Here's an pretty good sample I got from java2s:
import java.util.ArrayList; import java.util.List; /** List to array */ public class ToArray { public static void main(String[] args) { List list = new ArrayList(); list.add("Blobbo"); list.add("Cracked"); list.add("Dumbo"); // list.add(new Date()); // Don't mix and match! // Convert a collection to Object[], which can store objects // of any type. Object[] ol = list.toArray(); System.out.println("Array of Object has length " + ol.length); // This would throw an ArrayStoreException if the line // "list.add(new Date())" above were uncommented. String[] sl = (String[]) list.toArray(new String[0]); System.out.println("Array of String has length " + sl.length); } } Do you mean varargs to array? Just use it as an array, because it's already an array.
public void t(Object obj...) { Object obj0 = obj[0]; } Or you said about array from static list? Then use construction of initialization like Object objs = new Object[] {new Object, new Object, Object}.
Also, from List<T> to array use method List.toArray().
Or from varargs via List to array, just for fun:
public void t(Object obj...) { Object[] a = Arrays.asList(obj).toArray(); // or Object[] b = Arrays.asList(new Object, new Object).toArray(); } .
See this example :
public static void main(String[] args) { List<String> list = new ArrayList<String>(); list.add("a"); list.add("b"); list.add("c"); list.add("d"); list.add("e"); list.add("f"); String[] array = list.toArray(new String[0]); // Collection to array for (int i = 0; i < array.length; ++i) { String contents = array[i]; System.out.print(contents+"\t"); } System.out.println(); List<String> list2 = Arrays.asList(array); // Array back to Collection for (String s2 : list2) { String s3 = s2; System.out.print(s3+"\t"); } } You can either use ad-hoc array:
MyType[] arr = new MyType[]{new MyType(1), new MyType(2)...}; or create a little tricky method such as:
public static <T> T[] createArray(T... args) { return args; } Then use it like this:
MyType[] arr = createArray(new MyType(1), new MyType(2)...); MyType here is just any type that you want to create an array from. And three dots, just before the closing curly brace, are there just to show, that you may add as many MyType instances as you want. Try this for example: Object[] objArr = new Object[]{new Object(), new Object()};org.apache.commons.lang3.ArrayUtils T ArrayUtils.toArray(T... t); you welcome)