Results 21 to 33 of 33
Thread: Problem with method & return
- 03-19-2011, 11:03 PM #21
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Thanks. I didn't know you could return arrays for instance. I thought that only simple variable types could be returned...from another class, if that makes any sense....
Thank you Pbrockway2. Everything you said was very clear and made sense and fit precisely what I am trying to do. I am mostly trying to understand the specific syntax. For instance, at times you used <CLASSNAME> or even LIST and those are all new to me, so I was trying to figure it out, but otherwise everything you wrote was very useful and clear conceptually.
The code is supposed to do exactly what you said for now, which is to simply return all of the combinations and print them to the screen. Pretty simple. Thank you!
- 03-19-2011, 11:54 PM #22
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
Regarding "List<DiceCombo> ret = new ArrayList<DiceCombo>();"
A List is like an array, but it has useful methods. I only used the add() method which does the obvious thing. The list instance which is created (actually an ArrayList instance which is a sort of List) plays the role of the "bunch" which is created and returned.
Unlike an array a list grows as required which is a nice property.
Collections as a whole are dealt with in Oracle's Tutorial. But conceptually a list is so like an array that you can dive in and use it guided by the API docs linked to above.
The angle brackets are commonly used with collections like list. They can be read "of" so the line in question can be read as "Let ret be a List of DiceCombo and assign it a new ArrayList of DiceCombo".
- 03-20-2011, 03:23 PM #23
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
So I am looking at the API and I have another question. Does the <E> mean that you write the name of the list inside of the angle brackets or is it the name of the class? In other words, did you call it <DiceCombo> because that's the name of the DiceCombo class or is it just coincidental?
- 03-20-2011, 06:43 PM #24
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
No it not coincidental. What goes in the anglke brackets is what the list is a list of. In this case the list is declared to be a list of combinations.
- 03-20-2011, 07:46 PM #25
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Okay, so then why not just do?
ArrayList Name = new ArrayList();
I don't understand how and why List is used with ArrayList. Also, I still don't think I get the brackets. So do you always need an object to use List?
- 03-20-2011, 10:33 PM #26
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
You can write "ArrayList name = new ArrayList()" but generally, nowadays, people would use the generic form and say what the list is a list of. The situation is somewhat like having the option of declaring everything as Object. (As an experiment try writing a program where everything is declared as Object - some languages allow this sort of approach but it is painful in a strongly typed language like Java)
In the code I posted the only thing I did with the things from the list was print them. And println() will accept any Object argument. Other times we may want to do things that are specific to a particular type (like call their methods): in those cases, unless we have said what the list is a list of, the compiler will not know that such method calls are legal.
Again name can be declared as specifically an ArrayList, or more generally as an instance of List. I chose the more general approach since the only thing I was going to do with the list was to add things to it. Of more importance here is that getCombos() is declared to return a List and not an ArrayList. This gives me the freedom to change the type of ret to other types that implement List (like LinkedList) without having to change any other class that uses this method. Other classes can continue to ask for and get a List without knowing or caring what sort of list it is that they are receiving.Last edited by pbrockway2; 03-20-2011 at 10:38 PM.
- 03-20-2011, 10:35 PM #27
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Oh okay. Thank you pbrockway2. This was very useful! I appreciate you taking the time to answer my questions.
- 03-20-2011, 10:36 PM #28
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
So how do I change the status of this thread to solved?
Last edited by bigsonny; 03-20-2011 at 10:42 PM.
- 03-21-2011, 12:12 AM #29
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
You're welcome.
I'm not sure how you change the thread: check under "thread tools" at the top.
- 03-21-2011, 12:20 AM #30
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Got it. Thanks!
btw, how long have you been programming in Java? Is this your main language, or do you use multiple languages?
- 03-21-2011, 01:52 AM #31
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
I've been using Java since 1.0.2b (which is quite a while ago!). I'm just a hobbyist and currently I'm working with JavaScript/Python (a google apps web site).
- 03-21-2011, 02:01 AM #32
Senior Member
- Join Date
- Nov 2010
- Posts
- 150
- Rep Power
- 3
Thanks. I hope to be as fluent as you are with Java one day. So While I am familiar with Javascript (in principle), what is the advantage of using Python? Why not use PHP or Perl? What advantages do you finding using this specific programming language?
- 03-23-2011, 03:46 AM #33
Moderator
- Join Date
- Feb 2009
- Location
- New Zealand
- Posts
- 4,537
- Rep Power
- 11
There was no reason at for for Python - except that Google apps supports it. What I am writing is pretty modest, so PHP would have done (but then I would have had to pay to host it...). I'm enjoying Python but - and equally so with JavaScript - I'm rather out of my element. It's a case of googling about every time I hit a snag. The result so far isn't very pretty, but it does seem to work!
Similar Threads
-
Problem with my return method
By braddy in forum New To JavaReplies: 4Last Post: 10-19-2010, 06:34 AM -
Not able to return the method value
By dmakshay2002 in forum Advanced JavaReplies: 11Last Post: 05-28-2010, 02:07 PM -
Method won't return value
By footyvino in forum New To JavaReplies: 2Last Post: 03-26-2010, 10:49 AM -
Method return type problem
By McChill in forum New To JavaReplies: 7Last Post: 05-05-2009, 09:21 PM -
Return value of method
By cachi in forum New To JavaReplies: 1Last Post: 08-01-2007, 08:23 AM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks