Results 1 to 7 of 7
- 05-25-2010, 01:45 AM #1
Member
- Join Date
- May 2010
- Posts
- 19
- Rep Power
- 0
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
- 05-25-2010, 02:10 AM #2
Senior Member
- Join Date
- Mar 2010
- Posts
- 266
- Rep Power
- 11
Java Code:for(int i = 0; i < list.size(); i ++) { Room r = (Room).list.get(i); r.doWhatever(); }
- 05-25-2010, 02:23 AM #3
Member
- Join Date
- May 2010
- Posts
- 19
- Rep Power
- 0
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..
- 05-25-2010, 03:14 AM #4
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 11
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);
Otherwise, if you absolutely must cast, you'll need change this:
Java Code:Rooms xxx = (Rooms).list.get(20);
Java Code:Rooms xxx = (Rooms)list.get(20); // no period after the cast.
- 05-25-2010, 03:27 AM #5
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 11
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.
- 05-25-2010, 04:10 AM #6
Member
- Join Date
- May 2010
- Posts
- 19
- Rep Power
- 0
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.
- 05-25-2010, 04:12 AM #7
Senior Member
- Join Date
- May 2010
- Posts
- 436
- Rep Power
- 11
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
-
What class functions like list of strings?
By artemff in forum New To JavaReplies: 3Last Post: 12-31-2009, 05:55 PM -
Which would be better List iterator or accessing through index?
By deepuhassan in forum Advanced JavaReplies: 2Last Post: 12-17-2009, 04:06 PM -
Creating a Defined Class with Member Functions
By New2Java in forum New To JavaReplies: 6Last Post: 08-05-2009, 10:05 PM -
problem in accessing array values of one class in to jframe class
By cenafu in forum AWT / SwingReplies: 8Last Post: 03-21-2009, 10:34 AM -
Accessing list out another class
By Preethi in forum New To JavaReplies: 23Last Post: 10-26-2008, 03:54 PM
Bookmarks