0

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

2
  • Arrays.asList(o1, o2, o3 etc).toArray() Commented Feb 28, 2012 at 7:54
  • 1
    Of course make sure you really want an object array for some (unfathomable) reason (stackoverflow.com/a/6105705/202214) Commented Feb 28, 2012 at 15:49

9 Answers 9

3

You can use constructions as String[] str = new String[] {"a", "b", "c"}

Sign up to request clarification or add additional context in comments.

1 Comment

String[] str = {"a", "b", "c"};
2

varargs are array!

in MyMethod(Param param, MyObject... args) you can assign MyObject[] array = args;

Comments

1

List.toArray() might be what you're looking for. Javadoc can be found here.

1 Comment

I think he meant a method, that takes varags as a parameter, and allows to construct an array from them.
1

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); } } 

2 Comments

So it must be objects of same type in the list?What if I need objects of different types
@Jim well, you can post another question about that. :)
1

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(); } 

.

Comments

0

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"); } } 

Comments

0

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)...); 

2 Comments

What is the ad hoc array in your first example.It doesn't compile for me
@Jim 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()};
0

You can assign the varargs to the same type array directly

static void intTest(int... v) { int[] arr = v; } static void objTest(Object... obj) { Object[] arr = obj; } 

Comments

0
org.apache.commons.lang3.ArrayUtils T ArrayUtils.toArray(T... t); 

you welcome)

1 Comment

Please edit your answer and add some context by explaining how your answer solves the problem, instead of posting code-only answer. From Review

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.