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

Tip: Looking for answers? Try searching our database.

mixed type collections

Thread view: 
meridian - 15 Mar 2007 15:31 GMT
In Java can you mix types in collections or arrays like in Python and
Ruby?
(eg like the Python and Ruby examples here...)

># Python
> class MyClass:
>     def __str__(self):
>       return "I'm an object of MyClass"
>
> myObj = MyClass()
>
> l = [0, 1.1, "2", "three", myObj]
>
> for thing in l:
>     print thing
>
> # output
> #
> # 0
> # 1.1
> # 2
> # three
> # I'm an object of MyClass
-------------------------------------------
># Ruby
> class MyClass
>     def to_s
>       return "I'm an object of MyClass"
>     end
> end
>
> myObj = MyClass.new
>
> l = [0, 1.1, "2", "three", myObj]
>
> for thing in l
>     puts thing
> end
>
> # output
> #
> # 0
> # 1.1
> # 2
> # three
> # I'm an object of MyClass

thanks for your help....
Ingo R. Homann - 15 Mar 2007 16:04 GMT
Hi,

I am not aware of the Python and Ruby syntax you posted, but of course
it is possible in Java as well:

Object[] os=new Object[]{"Hello World",new Integer(43)};

Having said this - I think that is not a good idea. The advantage of
Java over other (Script) languages is its strong and static type
information. (*)  I suppose, a solution with a common super class (or
interface) would be a better idea. But that can only be decided if we
knew more about your *real* problem.

Ciao,
Ingo

(*) Some people think the opposite: that static type information is an
disadvantage. After all it is only a question of your individual
preference. But if you think that static typing is a disadvatage, then
you should not use Java. It's just that easy.
meridian - 15 Mar 2007 16:29 GMT
Thanks Ingo,
It wasn't a real world problem. I'm trying to learn some Java & Ruby
(I normally use python).
So when Ruby's basic examples showed this:
a = [ 3.14159, "pie", 99 ]
a.type  » Array
a.length  » 3
a[0]  » 3.14159
a[1]  » "pie"
a[2]  » 99
a[3]  » nil
I thought, yep I can do that in python but I couldn't in Java.
I can now though... thanks.
Wasn't advocating any preference about typing etc.
Cheers
Steve
Wojtek - 15 Mar 2007 16:36 GMT
meridian wrote :
> Thanks Ingo,
> It wasn't a real world problem. I'm trying to learn some Java & Ruby
[quoted text clipped - 10 lines]
> I can now though... thanks.
> Wasn't advocating any preference about typing etc.

Ok, but what you are really doing here is using an array as a container
for properties. In Java you would create a class with these properties
and use get/set to access them.

That way, when you want to pass the container to a method, you get the
compiler to ensure that the right container (object) gets passed in.

In PHP, all you can do is "hope" that the right array was passed in.

If you use an Object array, you are back to hoping that the right
Object array gets passed in.

Signature

Wojtek :-)



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.