Still a beginner at crafting quality coding but have the zeal to learn more. Your help would be appreciated.
My Blog on Learning Analytics
~ Mansukh
jism maverick wrote:In the following code
When i compile and run the above it gives me this output [[I@f72617] rather than the desired 5 5 5 5 5 output.
Can you please explain it to me why is it giving the garbage output rather than the desired one of 55555?
Henry Wong wrote:
jism maverick wrote:In the following code
When i compile and run the above it gives me this output [[I@f72617] rather than the desired 5 5 5 5 5 output.
Can you please explain it to me why is it giving the garbage output rather than the desired one of 55555?
Basically, your code prints the int array -- and that is what it does. It is printing that it is an primitive int "I" array "[" with an identity hash of "f72617". If you want the elements printed instead, perhaps you should call System.out with the elements (in a loop) instead?
[EDIT: beaten to the answer again]
Henry
Arrays.fill(arr1,5);
Still a beginner at crafting quality coding but have the zeal to learn more. Your help would be appreciated.
My Blog on Learning Analytics
Mansukhdeep Thind wrote:Welcome to the ranch Maverick.
The code is doing what you asked it to do. I do not see any place where you have printed the elements of the list. Look carefully at the signature of Arrays.asList(array[]) method. What is its return type?
... Also the method signature has no return type
~ Mansukh
Campbell Ritchie wrote:The return type is not part of the method signature. And lots of methods have no return type. They use the keyword void instead.
~ Mansukh
Mansukhdeep Thind wrote:
Mansukhdeep Thind wrote:Welcome to the ranch Maverick.
The code is doing what you asked it to do. I do not see any place where you have printed the elements of the list. Look carefully at the signature of Arrays.asList(array[]) method. What is its return type?
Well, now that I know your name. Welcome Ashish.
... Also the method signature has no return type
Well, there is not even a single method written in Java language that does not have a return type.
Moreover, which method did I refer to while asking you the return type? See carefully.
The System.out.println(Arrays.asList(arr1)); method is being used to print the elements of the array.I do not see any place where you have printed the elements of the list.
My mistake here, on rechecking it found that it returns a fixed size List backed by the specific array or a list view of the specified array. I'm sorry, i confused your method return type for asList() with the fill method.Look carefully at the signature of Arrays.asList(array[]) method. What is its return type?
Still a beginner at crafting quality coding but have the zeal to learn more. Your help would be appreciated.
My Blog on Learning Analytics
~ Mansukh
Mansukhdeep Thind wrote:So have you now understood why you were getting the hashed value instead of the actual elements that are stored in the list? Also, I hope you do know what a backed up list means.
Still a beginner at crafting quality coding but have the zeal to learn more. Your help would be appreciated.
My Blog on Learning Analytics
~ Mansukh
Jesper de Jong wrote:When you call
you get something else than you think. You probably think this returns a List with five elements, the integers which were in the original array. But it doesn't. Instead, it returns a List with a single element, and that single element is the array that you passed to Arrays.asList.
Let's carefully look at the output: [[I@26f44031]
The red square brackets come from List.toString.
The blue part is what you get if you convert an int[] to a string.
So, what you see here is the printout of a list that contains one int[] as its single element.
Instead of Arrays.asList, use Arrays.toString:
Still a beginner at crafting quality coding but have the zeal to learn more. Your help would be appreciated.
My Blog on Learning Analytics
Mansukhdeep Thind wrote:you are simply printing the address of the integer primitive stored in the returned list
Jeff Verdegan wrote:
Mansukhdeep Thind wrote:you are simply printing the address of the integer primitive stored in the returned list
No, because 1) It's not the address, and 2) The list doesn't hold an integer primitive.
~ Mansukh