Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > mixed type collections

Reply
Thread Tools

mixed type collections

 
 
meridian
Guest
Posts: n/a
 
      03-15-2007

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

 
Reply With Quote
 
 
 
 
Ingo R. Homann
Guest
Posts: n/a
 
      03-15-2007
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.

 
Reply With Quote
 
 
 
 
meridian
Guest
Posts: n/a
 
      03-15-2007

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

 
Reply With Quote
 
Wojtek
Guest
Posts: n/a
 
      03-15-2007
meridian wrote :
> 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.


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.

--
Wojtek


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Unable to cast object of type 'System.Web.UI.WebControls.SqlDataSource' to type 'System.Collections.IEnumerable'. Chris ASP .Net 1 02-12-2007 04:42 PM
How do I restrict the type of a text node in a mixed, complex-type element? Chishun Kwong XML 0 03-03-2005 05:09 PM
Re: Type Safe Collections? Ryan Stewart Java 9 06-06-2004 01:13 AM
Sorting collections based on nested collections Doug Poland Java 9 09-27-2003 10:46 PM
InnerProperty Persistance for Collections containing other Collections mutex ASP .Net Building Controls 0 07-27-2003 02:45 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57