| Author |
Return an arraylist of integers
|
jarco voorhees
Greenhorn
Joined: Sep 09, 2010
Posts: 17
|
|
Hello,
I have 2 classes in my program.
From one I pass a number to the second class.
The second class does some calculations on it and stores the results in a Arraylist<integer>.
When i try to return the arraylist to the first class it gives a weird result. (Fibonacci@19821f instead of the value's )
How can I pass it correctly?
|
My Blog (Dutch) http://jarco.be -
"Weaseling out of things is important to learn. It's what separates us from the animals... except the weasel."
|
 |
Mohamed Sanaulla
Bartender
Joined: Sep 08, 2007
Posts: 2928
|
|
This particular line:
prints the class name along with the hashcode value unless you have overridden the toString() method to return some meaningful String representation.
If you are using just the reference name in the println method- You should be overriding toString() to return a meaning String value for that instance.
|
Mohamed Sanaulla | My Blog
|
 |
jarco voorhees
Greenhorn
Joined: Sep 09, 2010
Posts: 17
|
|
mohamed sanaullah wrote:This particular line:
prints the class name along with the hashcode value unless you have overridden the toString() method to return some meaningful String representation.
If you are using just the reference name in the println method- You should be overriding toString() to return a meaning String value for that instance.
Hello,
I have a trouble understanding your answer.
Where exactly is the toString() used?
Is there a way to return a arraylist of integers?
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
You probably want this:
|
"Any fool can write code that a computer can understand. Good programmers write code that humans can understand." --- Martin Fowler
Please correct my English.
|
 |
jarco voorhees
Greenhorn
Joined: Sep 09, 2010
Posts: 17
|
|
Wouter Oet wrote:You probably want this:
Just to be sure I understand it:
It IS possible to return an arraylist<Integer>
When you have to print it you have to Put it in another Arraylist<Integer>
This is standard practice in Java? Or is it frowned upon?
Edit: The code you provided prints: []
|
 |
Wouter Oet
Saloon Keeper
Joined: Oct 25, 2008
Posts: 2700
|
|
jarco voorhees wrote:Just to be sure I understand it:
It IS possible to return an arraylist<Integer>
When you have to print it you have to Put it in another Arraylist<Integer>
This is standard practice in Java? Or is it frowned upon?
It returns a reference to an ArrayList<Integer>. So yea that is possible. You don't have to assign it to a reference variable before using it.
You could also do this:
|
 |
jarco voorhees
Greenhorn
Joined: Sep 09, 2010
Posts: 17
|
|
Wouter Oet wrote:
jarco voorhees wrote:Just to be sure I understand it:
It IS possible to return an arraylist<Integer>
When you have to print it you have to Put it in another Arraylist<Integer>
This is standard practice in Java? Or is it frowned upon?
It returns a reference to an ArrayList<Integer>. So yea that is possible. You don't have to assign it to a reference variable before using it.
You could also do this:
I am sorry but I am still struggling to understand this. Both your options return empty brackets. Why is that?
|
 |
Joanne Neal
Rancher
Joined: Aug 05, 2005
Posts: 3011
|
|
jarco voorhees wrote:I am sorry but I am still struggling to understand this. Both your options return empty brackets. Why is that?
Because your ArrayList is empty. Check the logic of the for loop in the bereken method.
|
Joanne
|
 |
jarco voorhees
Greenhorn
Joined: Sep 09, 2010
Posts: 17
|
|
Joanne Neal wrote:
jarco voorhees wrote:I am sorry but I am still struggling to understand this. Both your options return empty brackets. Why is that?
Because your ArrayList is empty. Check the logic of the for loop in the bereken method.
Oops I feel really stupid now.
I will change the loop so it works and then change the tester around until I am very sure I understand the reason behind it all.
|
 |
 |
|
|
subject: Return an arraylist of integers
|
|
|