aspose file tools
The moose likes Java in General and the fly likes ConcurrentLinkedQueue declaration problem Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Java » Java in General
Reply Bookmark "ConcurrentLinkedQueue declaration problem" Watch "ConcurrentLinkedQueue declaration problem" New topic
Author

ConcurrentLinkedQueue declaration problem

Kaarthick Ramamoorthy
Greenhorn

Joined: Sep 01, 2006
Posts: 23
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.
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24061
    
  13

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!


[Jess in Action][AskingGoodQuestions]
Jim Yingst
Wanderer
Sheriff

Joined: Jan 30, 2000
Posts: 18670
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.


"I'm not back." - Bill Harding, Twister
Kaarthick Ramamoorthy
Greenhorn

Joined: Sep 01, 2006
Posts: 23
Thank you Ernest & Jim.
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: ConcurrentLinkedQueue declaration problem
 
Similar Threads
Removing brackets from arraylist printout.
Generics
Doubt in JSTL
How can I manually add values to ArrayList[]
splitting arrays