• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Jeanne Boyarsky
  • Ron McLeod
Sheriffs:
  • Paul Clapham
  • Liutauras Vilda
  • Devaka Cooray
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

ConcurrentLinkedQueue declaration problem

 
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,



I would like to have array of queues that contains arrylist of strings. When I tried to declare as mentioned above, I could not.

The error thrown is, "cannot create a generic array of ConcurrentLinkedQueue<ArrayList<String>>"

Please help me.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yep, you cannot construct an array of a parameterized type. You have to create the array as the unparameterized version, then cast it, and live with the warning:

ConcurrentLinkedQueue<ArrayList<String>>[] queue =
(ConcurrentLinkedQueue<ArrayList<String>>[]) new ConcurrentLinkedQueue[5];

Instead of using an array, use a collection!
 
Wanderer
Posts: 18671
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rather than "live with the warning", you can put the offending code in a small private method, then tell the compiler to ignore that particular warning using the SuppressWarnings annotation:

It's also possible to put SuppressWarnings on the entire class, but that's usually not a good idea, as these warnings are a good thing in general, and you don't want to start ignoring all warnings.

But really, you're much better off just following EFH's later advice:

[EFH]: Instead of using an array, use a collection!

Arrays and generics don't mix well. Collections and generics do. If you're using generics at all, prefer collections whenever possible.
 
Kaarthick Ramamoorthy
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Ernest & Jim.
 
If you send is by car it's a shipment, but if by ship it's cargo. This tiny ad told me:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic