This week's book giveaway is in the Agile forum.
We're giving away four copies of Fixing your Scrum and have Ryan Ripley & Todd Miller on-line!
See this thread for details.
Win a copy of Fixing your Scrum this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

access object in a vector

 
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author
Posts: 4278
34
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
mark smith
Ranch Hand
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
author
Posts: 4278
34
jQuery Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya i use java 1.5



 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 263
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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?
 
I am displeased. You are no longer allowed to read this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic