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.
When you post actual code, always UseCodeTags (<- link to read). I have added them for you this time. And welcome to the Ranch
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.
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.
- X 2
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.
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 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?
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.
int reversedNumbers = reverseArray(numbers);
Then you can print the new reversedNumbers array.