Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes access object in a vector Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "access object in a vector" Watch "access object in a vector" New topic
Author

access object in a vector

mark smith
Ranch Hand

Joined: Apr 05, 2005
Posts: 246
hi

i have a vector and i put some PrintWriter to it...
i do that to be able to send message to all client connected to a server

i would like to put a msg to each PrintWriter



i get

ServeurMulti.java:20: cannot find symbol
symbol : method println(java.lang.String)
location: class java.lang.Object
vecConnection.elementAt(i).println("msg:" + msg);


thanks
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

References stored in Vectors are implicitly upcast to type Object, so when you pull them back out, you need to explicitly downcast them back to their correct type.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3702
    
    5

And just to nitpick...

It is strongly recommended you add brackets {} around the statements in your for() loop. Especially since you may need to add a line to cast them (although not required).

Second, don't use Vector, use ArrayList. Vector is used only when synchronization is required which can still be accomplished using ArrayLists with synchronized access methods.

Finally, if this is a public method you should probably check that the Vector itself is not null unless you've got very good encapsulation for modifying it.


My Blog: Down Home Country Coding with Scott Selikoff
mark smith
Ranch Hand

Joined: Apr 05, 2005
Posts: 246
ok i tried this



why i try to insert a PrintWriter object into the vector

the proram crash

java said:

ServeurMultiThread.java:27: warning: [unchecked] unchecked call to add(E) as a member of the raw type java.util.Vector
vecConnection.add(out);
^
Scott Selikoff
Saloon Keeper

Joined: Oct 23, 2005
Posts: 3702
    
    5

Could you post the code that the system is throwing the error on, specifically the lines surrounding "vecConnection.add(out);"?

If you are using Java 1.5, you should use generics when you create the array.
mark smith
Ranch Hand

Joined: Apr 05, 2005
Posts: 246
ya i use java 1.5



marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

As Scott indicated, this is related to generics in Java 1.5. Collections now use generics to safeguard the types of references they contain, and the compiler is simply warning you of potentially unsafe operations.

In this case, your type is not just Vector. It's Vector<PrintWriter>. For example...

Vector<PrintWriter> myWriters = new Vector<PrintWriter>();

(The message you posted above looks like what you would get after recompiling with -Xlint to show details of the warnings.)
[ December 07, 2005: Message edited by: marc weber ]
mark smith
Ranch Hand

Joined: Apr 05, 2005
Posts: 246
Originally posted by marc weber:
[QB]As Scott indicated, this is related to generics in Java 1.5. Collections now use generics to safeguard the types of references they contain, and the compiler is simply warning you of potentially unsafe operations.

In this case, your type is not just Vector. It's Vector<PrintWriter>. For example...

Vector<PrintWriter> myWriters = new Vector<PrintWriter>();

(The message you posted above looks like what you would get after recompiling with -Xlint to show details of the warnings.)



ok now with you line Vector... the program don't crash...
ya i recompiling with -Xlint and got the same warning...




what is the best way to have access to sendMsgToAll method?
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: access object in a vector
 
Similar Threads
My first interface
Simple Client Socket
java.lang.IllegalStateException
PrintWriter please help
setting a string from a Vector