Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Returning array from method  RSS feed

 
Tom Gibbins
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

I'm generating 20 random digits in an array in the main method. From here, i invoke the "reverseArray" method to reverse the numbers, then return them to main. Problem is i only get gibberish back e.g. "[I@6d06d69c". 1) Why do i see these numbers? What do they mean? 2) Where in my code have i done a mistake?
Thanks in advance! Constructive criticism is greatly appreciated.
 
Liutauras Vilda
Marshal
Posts: 4636
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

When you post actual code, always UseCodeTags (<- link to read). I have added them for you this time. And welcome to the Ranch
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That's not the fault of your method. You would get the same if you attempted to print any array.
Try using Arrays.toString().

About the String [I@6d06d69c you get: take a look at javadoc of Object.toString().
The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

In case of int[] type the class name is [I.

Minor points:
Try fixing indentation in your code (line 27).
I would not expect a method that reverses an array to print anything.
I suggest you always use curly braces, even if you have only one statement inside fors, ifs, elses, etc.
 
Liutauras Vilda
Marshal
Posts: 4636
316
BSD
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lets start with other issues first.

1. Your "main" method is too long. Ideally should be 1 line or so. Check this faq (<- link to read).
2. Line 5. When you use arrays, never choose variable names which contains word "List". It might means nothing to you, but it means something to others and it could be misleading. Because there are List class in Java, and usually it is a bad practice to use List variable name along with Array class.
3. Lines 11, 12. Never ever omit curly braces even if loop contains 1 statement in it. It is error prone and bad habit from the early times. Later will be difficult to get rid of it.
4. Your code indentation and formatting is not consistent. Sometimes you leave an empty line in between statements, sometimes not. Here is the Oracle coding convention (<- link). Bear in mind, last update was back in 1999, quite old, but still follow better style than yours code. Read about it and fix your code.
5. And why it prints not what you expecting. Because "println" method calls "toString" method to convert an object to a string representation. The thing is, that Array class doesn't override "toString" method which is inherited from the base class (Object), so it calls Object class toString. You can print array elements by accessing each separately with loop as you did with your original array or you can use statement "System.out.println(Arrays.toString(reverseArray(myList)));" which looks dreadfully long.

And one more thing:
As you see, on line 1, you write [] next to the data type, on line 3 you right next to the variable name. Stick to the one way of writing it. Better stick to the way how it is written on 1st line. Because, if for instance you'd change first line to the variable arguments "varargs":
So, always write next to the type of data.
 
Tom Gibbins
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks, both of you - i'm learning a lot from this. I tried to rewrite the code, changed some "bad habits" (changed array name to something other than list, added blocks for for loops, cleaned up the code here and there). This is the new code:



There is still something i don't understand here:

1) when i run this, i am not getting the reversed array from line 14. The reversed line is printed in line 23 however. If line 23 contains the reversed array, is that not what is being sent in line 24?
2) more improvements i can do to my code? I realize my main method probably is too big, but how i can reduce this to 1-2 sentences in main i have no idea. Or would a better way be to create another method to produce the inital array in the first way and then only print this 'initial array' and 'reversed array' in main?
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is because reverseArray() method doesn't alter its parameter.
It creates a new array and stores values inside it in reversed order.
To be able to print this array inside your main method you need to pass the array returned by reverseArray() to Arrays.toString().
 
Tom Gibbins
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:This is because reverseArray() method doesn't alter its parameter.
It creates a new array and stores values inside it in reversed order.
To be able to print this array inside your main method you need to pass the array returned by the method to Arrays.toString().


Thanks, but to do that i need to change this method to a String method? If i do that and return an array with String value, won't this prevent me from doing additional mathematical work on the array of a String type?
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tom Gibbins wrote:Thanks, but to do that i need to change this method to a String method?

No, you don't need to change your reverseArray() method. You need to use the value returned by the method.
You are not doing it in your code.

Also, note that the name of the method reverseArray is misleading.
It is not actually reversing the array passed to it. It creates a new array with elements in reverse order.
I would name it getReversedArray() or getReversed() if you prefer shorter name.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
On line 24, you return the new array back to the line where the method was called. However, back on that line, you don't catch it. it's basically lost. You need to do something like this:

int reversedNumbers[] = reverseArray(numbers);

Then you can print the new reversedNumbers array.
 
Tom Gibbins
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha, yes i see. I cannot dump the return value on the array that is already used. I have to make a new one to store the returns in. That did the trick, thank you all
 
Paweł Baczyński
Bartender
Posts: 2054
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, you could store the result in numbers if you needed:Also, to be technically correct, you are not creating a new array reversedNumbers. The creation of the array occurs inside reverseArray() method (by using new).
You are creating a new array variable.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!