Array of Objects

R

RFleming

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
 
C

christopher

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");


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];
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
 
L

Lew

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");


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];
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.

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'.
 

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,995
Messages
2,570,230
Members
46,816
Latest member
SapanaCarpetStudio

Latest Threads

Top