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

kleopatra
  • 51,061
  • 28
  • 99
  • 211
Jim
  • 18,826
  • 34
  • 135
  • 254

9 Answers9

3

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

anstarovoyt
  • 7,956
  • 2
  • 27
  • 35
2

varargs are array!

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

Nishant
  • 54,584
  • 13
  • 112
  • 127
1

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

Andrew Logvinov
  • 21,181
  • 6
  • 52
  • 54
  • I think he meant a method, that takes varags as a parameter, and allows to construct an array from them. – jFrenetic Feb 28 '12 at 07:57
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);
  }
}
ApprenticeHacker
  • 21,351
  • 27
  • 103
  • 153
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();
}

.

Nikolay Antipov
  • 920
  • 2
  • 8
  • 17
0
org.apache.commons.lang3.ArrayUtils
T ArrayUtils.toArray(T... t);

you welcome)

  • 3
    Please edit your answer and add some context by explaining how your answer solves the problem, instead of posting code-only answer. [From Review](https://stackoverflow.com/review/low-quality-posts/25052843) – Pedram Parsian Jan 09 '20 at 18:26
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");
    }
}   
gtiwari333
  • 24,554
  • 15
  • 75
  • 102
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)...);
jFrenetic
  • 5,384
  • 5
  • 42
  • 67
  • What is the ad hoc array in your first example.It doesn't compile for me – Jim Feb 28 '12 at 08:26
  • @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()};` – jFrenetic Feb 28 '12 at 09:15
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;
 }
clevertension
  • 6,929
  • 3
  • 28
  • 33