Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 6 of 6

Thread: Operate on list`s objects

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Question Operate on list`s objects

    Dear Friends...Hello..!
    Look at this example..:

    PHP Code:
    import java.util.ArrayList;
    ArrayList<Object> list = new ArrayList<Object>();
    list.
    add(10);    // boxing converts to instance of Integer
    list.add("Bisons");
    list.
    add(2.3);    // boxing converts to instance of Double

    for (Object o : list)
      
    System.out.println(o); 
    We just can print a list?
    I mean can`t operate on each object(depends on their types)?


  2. #2
    Member samfin's Avatar
    Join Date
    Dec 2010
    Location
    Manchester UK
    Posts
    37
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: Operate on list`s objects

    What is the problem you're having? I've put this into a class and it compiles and prints ok.

  3. #3
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Operate on list`s objects

    Quote Originally Posted by samfin View Post
    What is the problem you're having? I've put this into a class and it compiles and prints ok.
    yeah ..there`s no problem for printing..but..for example imagin that list[i] is belong to class Class1
    PHP Code:
    class Class1 {
    private 
    int a;

    can I use list[i].a ?
    or just can print list[i] ...?

  4. #4
    Member samfin's Avatar
    Join Date
    Dec 2010
    Location
    Manchester UK
    Posts
    37
    Thanks
    1
    Thanked 5 Times in 4 Posts

    Default Re: Operate on list`s objects

    I'm not following you, can you post some code with the lists, variables whatever, that shows what you're asking please

  5. #5
    Junior Member
    Join Date
    Feb 2011
    Posts
    6
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Operate on list`s objects

    Quote Originally Posted by samfin View Post
    I'm not following you, can you post some code with the lists, variables whatever, that shows what you're asking please
    It`s just an example..I mean what can i do with list`s objects except printing..!?

  6. #6
    Forum old-timer
    Join Date
    Nov 2008
    Location
    Faversham, Kent, UK
    Posts
    472
    My Mood
    Mellow
    Thanks
    4
    Thanked 58 Times in 54 Posts

    Default Re: Operate on list`s objects

    The short answer is don't mix object types in a list like that. You can only store objects of different class types if you store them as an Object, which means you can only retrieve them as an Object, so you're very limited as to what you can do with them.

    It is possible to use the Reflection API to discover the real type of the object you want to use, then cast it to that type to use it. I strongly advise against this, it should be the last resort when there is no alternative.

    There are far better alternatives, but it depends on exactly what you're trying to achieve. The ideal situation is to store a class type that has the methods that you want to use. This is usually done by defining an interface with the methods you want and having various classes implement the methods in their own way. Then the list can be specified to store the interface type and all classes that implement the interface can be used with it.

Similar Threads

  1. SpinnerListModel setList(List<?> list)
    By roy epperson in forum Collections and Generics
    Replies: 4
    Last Post: November 29th, 2010, 10:30 AM
  2. LinkedList Objects
    By thedolphin13 in forum What's Wrong With My Code?
    Replies: 11
    Last Post: October 13th, 2010, 03:14 PM
  3. Help with background and objects
    By Afromiffo in forum What's Wrong With My Code?
    Replies: 3
    Last Post: April 8th, 2010, 01:19 AM
  4. Objects
    By chronoz13 in forum Java Theory & Questions
    Replies: 5
    Last Post: January 20th, 2010, 12:50 PM
  5. Arrays Of Objects?
    By MysticDeath in forum Object Oriented Programming
    Replies: 2
    Last Post: October 20th, 2009, 09:39 PM