Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2005

Tip: Looking for answers? Try searching our database.

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

Thread view: 
Ulrich Scholz - 05 Dec 2005 14:00 GMT
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/



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.