Hi everybody.
In my project I create an array <code>atoms</code> in Java 5.0 that
holds elements of Type Vector<Term>. Term is some other class in my
project.
private Vector<Term>[] atoms;
In Eclipse, the assignment
atoms = new Vector[size];
gives the warning
Type safety: The expression of type Vector[] needs unchecked
conversion to conform to Vector<Term>[]
But
atoms = new Vector<Term>[size];
results in the error
Cannot create a generic array of Vector<Term>
How do I correctly initialize Vector <code> atoms</code> in Java 5.0?
Pointer to documentation welcome.
Thank you,
Uli
Ian Pilcher - 05 Dec 2005 15:26 GMT
> How do I correctly initialize Vector <code> atoms</code> in Java 5.0?
> Pointer to documentation welcome.
@SuppressWarnings("unchecked")
private static Vector<Term>[] createAtoms(int size)
{
return (Vector<Term>[])new Vector[size];
}
Alternatively, use an ArrayList<Vector<Term>>.

Signature
========================================================================
Ian Pilcher i.pilcher@comcast.net
========================================================================
Ulrich Scholz - 05 Dec 2005 16:07 GMT
Thanks, that seems like a good reference.
Although,
Vector<Term>[] atoms = new Vector<?>[10];
Eclipse gives the compile time error
Type mismatch: cannot convert from Vector<?>[] to Vector<Term>[]
while the document has the example
List<String>[] lsa = new List<?>[10]; // unchecked warning - this is
unsafe!
Warning, not an error.
Uli
Viator - 05 Dec 2005 15:27 GMT
Have a look at the document Section 7.3
http://java.sun.com/j2se/1.5/pdf/generics-tutorial.pdf
Hope it helps :-)
Amit
Ulrich Scholz - 05 Dec 2005 16:09 GMT
Sorry, I replied to the wrong comment. copy of the above:
Thanks, that seems like a good reference.
Although,
Vector<Term>[] atoms = new Vector<?>[10];
Eclipse gives the compile time error
Type mismatch: cannot convert from Vector<?>[] to Vector<Term>[]
while the document has the example
List<String>[] lsa = new List<?>[10]; // unchecked warning - this is
unsafe!
Warning, not an error.
Uli
Thomas Hawtin - 05 Dec 2005 17:26 GMT
> In my project I create an array <code>atoms</code> in Java 5.0 that
> holds elements of Type Vector<Term>. Term is some other class in my
> project.
>
> private Vector<Term>[] atoms;
Drop the use of reference arrays. Don't try to suppress errors or
warnings, if that can be avoided.
Just use:
private List<Vector<Term>> atoms;
atoms = new ArrayList<Vector<Term>>(num);
atoms.addAll(Collections.nCopies(num, null));
Tom Hawtin

Signature
Unemployed English Java programmer
http://jroller.com/page/tackline/