Reputation: 31
So, I was writing a Java program and made a method create that accepted Object datatype variable. Here is the brief look
class FILO
{
ArrayList listFILO;
public void create(Object input)
{
Class<? extends Object> type=input.getClass();
if(type.isArray())
{
/*Problem starts here. I want to create an array with same datatype as
input but I can't get my head around it.*/
}
//...
}
}
Upvotes: 1
Views: 311
Reputation: 11030
You could use reflection, using java.lang.reflect.Array
.
Array.newInstance( input.getClass(), length );
But trying to use reflection like this is usually a bad idea. You want to require the user to enter the type you are asking for, and then check that the user has entered the correct type.
Upvotes: 1