Results 1 to 10 of 10
Thread: Method to return object
- 05-01-2012, 06:04 PM #1
Member
- Join Date
- Apr 2012
- Posts
- 10
- Rep Power
- 0
Method to return object
Hey, I am working on this CW where I have an Array list which has all the object saved in. And there is an unique ID assigned to every object. This isnt problem this works fine. My problem now is I need to make a method which will return object depending on the unique ID.
My question is how do i declare the method ? whatever I try it just says error "invalid method type"...
- 05-01-2012, 06:26 PM #2
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Method to return object
SHow us what you've tried, and the full text of any compilation errors.
Please do not ask for code as refusal often offends.
- 05-01-2012, 06:44 PM #3
Member
- Join Date
- Apr 2012
- Posts
- 10
- Rep Power
- 0
Re: Method to return object
well this is my method to add new player
void regPlayer(Player p){ //passing the player object
playerArray.add(p); // adding it to the arraylist
p.setReg(RegNum()); // gives the player object an unique ID
sort(); // this is just to sort the arraylist in alphabetical order
}
now i need another method which will return player object, depending on which player ID was passed into the method.
so something like
requestPlayer(int i){
return playerArray.get(i);
}
I am not sure how to actually do this. And when i hover over the requestPlayer its saying "invalid method declaration; return type required"
And the method requires a type. Or can I just leave it as void ?Last edited by Collider; 05-01-2012 at 06:50 PM.
- 05-01-2012, 06:53 PM #4
Member
- Join Date
- Apr 2012
- Posts
- 10
- Rep Power
- 0
Re: Method to return object
oh im dumb... im returning object Player so type has to be Player requestPlayer(int i) >.<
- 05-02-2012, 09:38 AM #5
Moderator
- Join Date
- Apr 2009
- Posts
- 10,484
- Rep Power
- 16
Re: Method to return object
There you go.
However, this will return the Player at index 'i' in the array:
In order to get the Player with the regNum == i you'll have to loop through the List and compare them, returning the one that matches.Java Code:return playerArray.get(i);
Please do not ask for code as refusal often offends.
- 05-02-2012, 11:58 AM #6
Member
- Join Date
- Apr 2012
- Posts
- 10
- Rep Power
- 0
Re: Method to return object
Yess thanks a lot =) that's what I have done. (do i mark this as solved somehow ?)
- 05-02-2012, 12:45 PM #7
Member
- Join Date
- Apr 2012
- Posts
- 10
- Rep Power
- 0
Re: Method to return object
haha people are stalking me here lol and its nearly done :P handing it in today
- 05-02-2012, 02:12 PM #8
Banned
- Join Date
- May 2012
- Posts
- 2
- Rep Power
- 0
Re: Method to return object
what is this?!?!
- 05-02-2012, 02:50 PM #9
Banned
- Join Date
- May 2012
- Posts
- 2
- Rep Power
- 0
Re: Method to return object
you boys are in serious trouble!!
- 05-02-2012, 03:35 PM #10
Member
- Join Date
- Apr 2012
- Posts
- 10
- Rep Power
- 0
Similar Threads
-
About the method which return an object
By nihirus in forum New To JavaReplies: 6Last Post: 02-22-2012, 12:10 AM -
Return custom object in Java Webservice
By Nela in forum JavaServer Pages (JSP) and JSTLReplies: 1Last Post: 01-23-2012, 04:58 PM -
Return an object to use in another method
By TidusSolan in forum New To JavaReplies: 3Last Post: 03-19-2009, 08:00 PM -
How I can return an object?
By CFW in forum New To JavaReplies: 4Last Post: 01-14-2009, 10:54 AM -
how to return an object from an arraylist
By elizabeth in forum New To JavaReplies: 1Last Post: 07-30-2007, 06:57 PM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks