-1

I builded a class to act like a queue. And I called it SalehQueue of Generic type. How can I create an array of that class?

  SalehQueue<String> department=new SalehQueue<String>();

Can I do this?

  SalehQueue[]<String> department=new SalehQueue[10]<String>();

Any idea??

Saleh Makkawy
  • 49
  • 1
  • 7

3 Answers3

0

Yes you can do that, instatiate an Array of objects.

If you cant create generic typed arrays, just omit the generic type on instantiation.

List<String>[] stringss = new ArrayList[10];
Marcos Vasconcelos
  • 17,773
  • 29
  • 104
  • 165
0
 ArrayList <SalehQueue<String>> department=  new ArrayList <SalehQueue<String>>();

This is the correct answer. Because Java does not support array in generic.

Saleh Makkawy
  • 49
  • 1
  • 7
0

When using new to create an array, the component type must be a refillable type.

You can create the array using the raw type:

SalehQueue<String>[] department = new SalehQueue[10];

or the type parameterized by all wildcards:

SalehQueue<String>[] department = (SalehQueue<String>[])new SalehQueue<?>[10];
newacct
  • 110,405
  • 27
  • 152
  • 217