How to initialize variable of type Vector<Term>[] in Java 5.0?

U

Ulrich Scholz

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
 
I

Ian Pilcher

Ulrich said:
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>>.
 
U

Ulrich Scholz

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
 
U

Ulrich Scholz

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
 
T

Thomas Hawtin

Ulrich said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top