Results 1 to 7 of 7
  1. #1
    ruerric is offline Member
    Join Date
    May 2010
    Posts
    19
    Rep Power
    0

    Default Accessing class functions in a list..

    Hello,

    I created a custom class called Rooms with all the appropriate setters and getters.

    I iterated through a csv file and input all of the data into my custom object class and then put that object into a list by using list.add().

    Now I want to access the class methods through the use of the list.. how can I do so?

    -ET

  2. #2
    iluxa is offline Senior Member
    Join Date
    Mar 2010
    Posts
    266
    Rep Power
    5

    Default

    Java Code:
    for(int i = 0; i < list.size(); i ++) {
      Room r = (Room).list.get(i);
      r.doWhatever();
    }

  3. #3
    ruerric is offline Member
    Join Date
    May 2010
    Posts
    19
    Rep Power
    0

    Default

    I'm getting a minor error.. probably because of Syntax?

    How I declared my list:
    List list = new ArrayList<Rooms>(); // List implemented as growable array

    Rooms xxx = (Rooms).list.get(20);

    Says illegal start of type error..

  4. #4
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    If you're going to use generics, don't use them half-way. Make your list a List<Rooms> :
    Java Code:
    List<Rooms> list = new ArrayList<Rooms>();
    //....
    Rooms myRoom = list.get(20);
    Then no need to cast at all.

    Otherwise, if you absolutely must cast, you'll need change this:
    Java Code:
    Rooms xxx = (Rooms).list.get(20);
    to this:
    Java Code:
    Rooms xxx = (Rooms)list.get(20); // no period after the cast.

  5. #5
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Cross-post: New To Java - Accessing class functions in a list

    OP, keep cross-posting without telling us and no one is going to want to answer your question on this or other fora. Just FYI.

  6. #6
    ruerric is offline Member
    Join Date
    May 2010
    Posts
    19
    Rep Power
    0

    Default

    Sorry about that, I didn't know that it was frowned upon to do so. It's my first time posting for help and I didn't know which forums would get me a speedier reply. Thanks for the heads up.

  7. #7
    curmudgeon is offline Senior Member
    Join Date
    May 2010
    Posts
    436
    Rep Power
    5

    Default

    Quote Originally Posted by ruerric View Post
    Sorry about that, I didn't know that it was frowned upon to do so. It's my first time posting for help and I didn't know which forums would get me a speedier reply. Thanks for the heads up.
    Thanks for the understanding. To know more about why this bugs many, please look here: JavaRanch - Be Forthright When Cross Posting To Other Sites

Similar Threads

  1. What class functions like list of strings?
    By artemff in forum New To Java
    Replies: 3
    Last Post: 12-31-2009, 05:55 PM
  2. Replies: 2
    Last Post: 12-17-2009, 04:06 PM
  3. Creating a Defined Class with Member Functions
    By New2Java in forum New To Java
    Replies: 6
    Last Post: 08-05-2009, 10:05 PM
  4. Replies: 8
    Last Post: 03-21-2009, 10:34 AM
  5. Accessing list out another class
    By Preethi in forum New To Java
    Replies: 23
    Last Post: 10-26-2008, 03:54 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •