Skip to Main Content

Java APIs

Announcement

For appeals, questions and feedback about Oracle Forums, please email oracle-forums-moderators_us@oracle.com. Technical questions should be asked in the appropriate category. Thank you!

Interested in getting your voice heard by members of the Developer Marketing team at Oracle? Check out this post for AppDev or this post for AI focus group information.

How to create an array of generics?

843793Oct 19 2004 — edited Jun 18 2008
I have the following snippet of code using array of vectors:

Vector[] tmpArr = new Vector[n];
for(int j=0; j<n; j++)
{
tmpArr[j] = new Vector();
....
String str = ...
....
if (!tmpArr[j].contains(str))
tmpArr[j].add(str);
}

And I want to convert to generics:

Vector<String>[] tmpArr = new Vector<String>[n];
for(int j=0; j<n; j++)
{
tmpArr[j] = new Vector<String>();
....
String str = ....
....
if (!tmpArr[j].contains(str))
tmpArr[j].add(str);
}


But the first row gives me an error:
-->Generic array creation.

If I change it in
Vector<String>[] tmpArr = new Vector<String>[n];
(as I've seen in a pdf by G.Bracha talking aout collections)
it gives me the error:
-->cannot find symbol
-->method add(String)

in the
tmpArr[j].add(str);
row.

The only way it seems to work is
Vector<String>[] tmpArr = new Vector[n];
but it gives me the unchecked conversion warning.

How can I create an array of generics?

Thank you!
Matteo
Comments
Locked Post
New comments cannot be posted to this locked post.
Post Details
Locked on Jul 16 2008
Added on Oct 19 2004
12 comments
463 views