mixed type collections

Discussion in 'Java' started by meridian, Mar 15, 2007.

  1. meridian

    meridian Guest

    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....
    meridian, Mar 15, 2007
    #1
    1. Advertising

  2. 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.
    Ingo R. Homann, Mar 15, 2007
    #2
    1. Advertising

  3. meridian

    meridian Guest

    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
    meridian, Mar 15, 2007
    #3
  4. meridian

    Wojtek Guest

    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 :)
    Wojtek, Mar 15, 2007
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Doug Poland
    Replies:
    9
    Views:
    724
    VisionSet
    Sep 27, 2003
  2. Ryan Stewart

    Re: Type Safe Collections?

    Ryan Stewart, May 31, 2004, in forum: Java
    Replies:
    9
    Views:
    355
    xarax
    Jun 6, 2004
  3. Chishun Kwong
    Replies:
    0
    Views:
    415
    Chishun Kwong
    Mar 3, 2005
  4. Chris
    Replies:
    1
    Views:
    975
    Teemu Keiski
    Feb 12, 2007
  5. mutex
    Replies:
    0
    Views:
    206
    mutex
    Jul 27, 2003
Loading...

Share This Page