• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Campbell Ritchie
  • Tim Cooke
  • Bear Bibeault
Sheriffs:
  • Paul Clapham
  • Junilu Lacar
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Ganesh Patekar
  • Tim Moores
  • Pete Letkeman
  • Stephan van Hulst
Bartenders:
  • Carey Brown
  • Tim Holloway
  • Joe Ess

Return an arraylist of integers  RSS feed

 
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?






 
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?


 
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?
 
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.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!