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 / June 2007

Tip: Looking for answers? Try searching our database.

Array of Objects

Thread view: 
RFleming@NationalSteel.com - 17 Jun 2007 05:10 GMT
Hello,

   I am somewhat new to JAVA, but not to programming in general.  I
recently wrote a class that uses the NIO Socket class.  I would like
to now make this class handle multiple sockets.  can I do this?  For
example:  Socket[] socketarray = null;    socketarray[1]
("127.0.0.1","8000");
If so I also can not seem to figure out how to do.....   Socket
tempsocket = socketarray[1];
I have not seen any examples of arrays that appear to use Class
objects such as the Socket, just using primitive types such as ints,
bools, etc.....  Any help would be greatly appreciated!

Thanks

Ryan
christopher@dailycrossword.com - 17 Jun 2007 06:28 GMT
On Jun 16, 9:10 pm, RFlem...@NationalSteel.com wrote:
> Hello,
>
[quoted text clipped - 3 lines]
> example:  Socket[] socketarray = null;    socketarray[1]
> ("127.0.0.1","8000");

Socket[] socketArray = new Socket[13];
socketArray[1]= new Socket("127.0.0.1","8000");
but I would use a LinkedList or Vector or something -- arrays are not
particularly useful.

> If so I also can not seem to figure out how to do.....   Socket
> tempsocket = socketarray[1];
[quoted text clipped - 5 lines]
>
> Ryan
Lew - 17 Jun 2007 15:18 GMT
> On Jun 16, 9:10 pm, RFlem...@NationalSteel.com wrote:
>> Hello,
[quoted text clipped - 14 lines]
>> I have not seen any examples of arrays that appear to use Class
>> objects such as the Socket, just using primitive types such as ints,

A Socket is not a Class object.  An instance of Socket is a Socket object.
Class is a different class from Socket.

>> bools, etc.....  Any help would be greatly appreciated!

Arrays of reference types are extremely common.  Every basic tutorial includes
at least a mention of them.  For example, the basic Sun tutorial
<http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html>
mentions
  String[] anArrayOfStrings;
and
  String[][] names = {{"Mr. ", "Mrs. ", "Ms. "},
                      {"Smith", "Jones"}};

However, they do not seem to go into the details of how to new and array.  One
can always go to the JLS for the final word:
<http://java.sun.com/docs/books/jls/third_edition/html/arrays.html>
in particular
<http://java.sun.com/docs/books/jls/third_edition/html/arrays.html#10.3>
which links to
<http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.10>
which shows you that the syntax for array creation is precisely the same for
primitives as it is for reference types.

Isn't the JLS useful?

So having the knowledge of how to create an array of primitives confers
automatically the knowledge of how to create an array of references.  Simply
use the 'ClassOrInterfaceType' in lieu of the 'PrimitiveType'.

Signature

Lew



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.