Quantcast
Channel: How can I create a generic array in Java? - Stack Overflow
Browsing all 66 articles
Browse latest View live

Answer by Irfan Ul Haq for How to create a generic array in Java?

Generic array creation is disallowed in java but you can do it like class Stack<T> { private final T[] array; public Stack(int capacity) { array = (T[]) new Object[capacity]; } }

View Article



Answer by Rodrigo Asensio for How to create a generic array in Java?

Passing a list of values... public <T> T[] array(T... values) { return values; }

View Article

Answer by saka1029 for How to create a generic array in Java?

You do not need to pass the Class argument to the constructor. Try this. public class GenSet<T> { private final T[] array; @SuppressWarnings("unchecked") public GenSet(int capacity, T... dummy) {...

View Article

Answer by Crab Nebula for How to create a generic array in Java?

I actually found a pretty unique solution to bypass the inability to initiate a generic array. What you have to do is create a class that takes in the generic variable T like so: class GenericInvoker...

View Article

Answer by Nikos for How to create a generic array in Java?

I have found a quick and easy way that works for me. Note that i have only used this on Java JDK 8. I don't know if it will work with previous versions. Although we cannot instantiate a generic array...

View Article


Answer by developer747 for How to create a generic array in Java?

I found a sort of a work around to this problem. The line below throws generic array creation error List<Person>[] personLists=new ArrayList<Person>()[10]; However if I encapsulate...

View Article

Answer by Benjamin M for How to create a generic array in Java?

What about this solution? @SafeVarargs public static <T> T[] toGenericArray(T ... elems) { return elems; } It works and looks too simple to be true. Is there any drawback?

View Article

Answer by plugwash for How to create a generic array in Java?

No one else has answered the question of what is going on in the example you posted. import java.lang.reflect.Array; class Stack<T> { public Stack(Class<T> clazz, int capacity) { array =...

View Article


Answer by Pedram Esmaeeli for How to create a generic array in Java?

Actually an easier way to do so, is to create an array of objects and cast it to your desired type like the following example: T[] array = (T[])new Object[SIZE]; where SIZE is a constant and T is a...

View Article


Answer by Zubair Ibrhaim for How to create a generic array in Java?

private E a[]; private int size; public GenSet(int elem) { size = elem; a = (E[]) new E[size]; }

View Article

Answer by samir benzenine for How to create a generic array in Java?

You could use a cast: public class GenSet<Item> { private Item[] a; public GenSet(int s) { a = (Item[]) new Object[s]; } }

View Article

Answer by Cambot for How to create a generic array in Java?

I'm wondering if this code would create an effective generic array? public T [] createArray(int desiredSize){ ArrayList<T> builder = new ArrayList<T>(); for(int x=0;x<desiredSize;x++){...

View Article

Answer by Radiodef for How to create a generic array in Java?

In Java 8, we can do a kind of generic array creation using a lambda or method reference. This is similar to the reflective approach (which passes a Class), but here we aren't using reflection....

View Article


Answer by vnportnoy for How to create a generic array in Java?

The forced cast suggested by other people did not work for me, throwing an exception of illegal casting. However, this implicit cast worked fine: Item<K>[] array = new Item[SIZE]; where Item is a...

View Article

Answer by Mohsen Afshin for How to create a generic array in Java?

Maybe unrelated to this question but while I was getting the "generic array creation" error for using Tuple<Long,String>[] tupleArray = new Tuple<Long,String>[10]; I find out the following...

View Article


Answer by Jason C for How to create a generic array in Java?

To extend to more dimensions, just add []'s and dimension parameters to newInstance() (T is a type parameter, cls is a Class<T>, d1 through d5 are integers): T[] array =...

View Article

Answer by MatheusJardimB for How to create a generic array in Java?

Look also to this code: public static <T> T[] toArray(final List<T> obj) { if (obj == null || obj.isEmpty()) { return null; } final T t = obj.get(0); final T[] res = (T[])...

View Article


Answer by Bobster for How to create a generic array in Java?

I made this code snippet to reflectively instantiate a class which is passed for a simple automated test utility. Object attributeValue = null; try { if(clazz.isArray()){ Class<?> arrayType =...

View Article

Answer by StarMonkey for How to create a generic array in Java?

An easy, albeit messy workaround to this would be to nest a second "holder" class inside of your main class, and use it to hold your data. public class Whatever<Thing>{ private class...

View Article

Answer by irreputable for How to create a generic array in Java?

This is the only answer that is type safe E[] a; a = newArray(size); @SafeVarargs static <E> E[] newArray(int length, E... array) { return Arrays.copyOf(array, length); }

View Article
Browsing all 66 articles
Browse latest View live




Latest Images