I have a function that returns an Object. The Object can contain an array of primatives or an array of objects. In C# I can create an empty array of objects or primatives using code like:
Array values = Array.CreateInstance(/*Type*/type, /*int*/length);
Is there an equivalent in Java?
Assuming you only know the element type at execution time, I think you're looking for Array.newInstance
.
Object intArray = Array.newInstance(int.class, 10);
Object stringArray = Array.newInstance(String.class, 10);
(That will create an int[]
and a String[]
respectively.)
See more on this question at Stackoverflow