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 2 of 2

Thread: Dymaic Casting of List

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

    Question Dymaic Casting of List

    private static Void createTbody(List object) {

    Iterator hritr = object.iterator();

    while(hritr.hasNext()){

    UserDto users = (UserDto)hritr.next();
    users.userId;
    users.userName;

    }

    }


    DtoClass::

    UserDto

    public class UserDto {

    public String userName;
    public String userId;

    }

    In the above code i dont have any Issues.I can get list of userId and userName from the List.

    How i can cast the Object dynamically.

    UserDto users = (UserDto)hritr.next();
    in this code i dont want to put UserDto,this should by dynamic Class.
    Because the list may contain,List of users,List of Customers etc.

    How this can be achieved in java..Can i use reflection?
    Please help me to overcome this issue.


  2. #2

    Default Re: Dymaic Casting of List

    The easiest way is to use inheritance. The best way is to use object composition.

    How will you decide which object you want to cast it into? Why does your "List object" not contain a parameter. Techincally, you can put anything inside a list, what makes this list unique? How do you determine what makes each user unique?

    Consider this code:
    DeskObjectIterator doi = new DeskObjectIterator(deskObjectArrayOrList);
     
    while(doi.hasNext())
    {
    	DeskObject deskObject = doi.next();
    	if(deskObject instanceof Pen)
    		Pen pen = (Pen)deskObject;
    	else if(deskObject instanceof Computer)
    		Computer computer = (Computer)deskObject;
    	// else if ...
    }

    or ...

    DeskObjectIterator doi = new DeskObjectIterator(deskObjectArrayOrList);
     
    while(doi.hasNext())
    {
    	DeskObject deskObject = doi.next();
    	if(deskObject.typeString.equals("Pen"))
    		Pen pen = (Pen)deskObject;
    	else if(deskObject.typeString.equals(Computer))
    		Computer computer = (Computer)deskObject;
    	// else if ...
    }
    Kenneth Walter
    Software Developer
    http://kennywalter.com

Similar Threads

  1. Class Casting
    By Sana1990 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 9th, 2012, 08:13 AM
  2. [SOLVED] why casting int to String is not possible through brackets method
    By voltaire in forum Java Theory & Questions
    Replies: 2
    Last Post: May 2nd, 2010, 04:00 PM
  3. reading string input then casting it to an int?
    By etidd in forum Java Theory & Questions
    Replies: 2
    Last Post: March 27th, 2010, 11:49 PM
  4. Performance of Type Casting and Conversions
    By fritzoid in forum Java Theory & Questions
    Replies: 1
    Last Post: October 1st, 2009, 07:56 PM
  5. Type casting error in Java
    By Eric in forum Java Theory & Questions
    Replies: 3
    Last Post: December 13th, 2008, 04:11 PM

Tags for this Thread