• Post Reply Bookmark Topic Watch Topic
  • New Topic

Return an arraylist of integers  RSS feed

 
jarco voorhees
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?






 
Mohamed Sanaulla
Bartender
Posts: 3185
34
Google App Engine Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jarco voorhees
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You probably want this:


 
jarco voorhees
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Bartender
Posts: 2700
IntelliJ IDE Opera
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3742
16
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
jarco voorhees
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!