• Post Reply Bookmark Topic Watch Topic
  • New Topic

array problem  RSS feed

 
Yakov Borukhov
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
for some reason, this returns an address instead of an array. can someone please help! am i not printing or returning an array correctly? thank you very much

 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it returns an array. You're making at least three faulty assumptions:

1) You're assuming that passing an array to println() will produce a particular format that you'd like to see. (In other words, you're assuming arrays override toString().)

2) You're assuming that the value you're seeing is an address. It may or may not be.

3) You're assuming that Java will just arbitrarily return something different than what you tell it to. That's not the case. Java does exactly what you tell it to.

Since arrays don't override toString(), they use the toString() inherited from Object. You can read the docs to find out what that does. You have two main choices: 1) Call java.util.Arrays.toString(), or 2) Iterate over the array and print out the individual elements yourself.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
You are in fact returning a reference to that array. What do you mean by printing an address? Do you mean getting output you don’t understand?

What you are probably getting is the un-overridden toString() method, because the only thing an array overrides is clone(). You could try the Arrays class which has a method to convert an array to a String, or writing your own method to print an array.
 
Campbell Ritchie
Marshal
Posts: 56546
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you been taking lessons from Rob, Jeff?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:Have you been taking lessons from Rob, Jeff?


I ate my Wheaties this morning!
 
Yakov Borukhov
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh ok. thank you very much, i solved the problem!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!