0

What's syntax for creating an array of Objects of some class type?

Object<SomeClassType<T>>[] array?

Mike
  • 23
  • 4

3 Answers3

1

Creating an array of a generic type is not allowed, but what you can do, is creating one with wildcard and casting it. The cast will give you a warning, but as the array only contains nulls by default, it can be done safely.

@SuppressWarnings("unchecked")
Object<SomeClassType<T>>[] array = (Object<SomeClassType<T>>[]) new Object<?>[length]
Bubletan
  • 3,693
  • 6
  • 23
  • 33
0

I would say something like this would allow you to create an array.

String[] testArray = new String[3];
testArray[0] = "one";
testArray[1] = "two";
testArray[2] = "three";

String[] testArray = {"one","two","three"};
Mykola
  • 3,152
  • 6
  • 20
  • 39
0

A number of ways to declare an array of objects of some class type. For example,

1. MyClass[] myArray = new MyClass[# of elements];

Then initialize each as, myArray[0] = new MyClass();, myArray[1] = new MyClass(), etc.. or via a for-loop

2. MyClass[] myArray = {new MyClass(), new MyClass(), ...};
rby
  • 718
  • 6
  • 10