aspose file tools*
The moose likes Beginning Java and the fly likes Arrays.fill method not working as desired. Help would be appreciated Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Arrays.fill method not working as desired. Help would be appreciated" Watch "Arrays.fill method not working as desired. Help would be appreciated" New topic
Author

Arrays.fill method not working as desired. Help would be appreciated

Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

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?

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
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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?


~ Mansukh
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18846
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

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


Dear Henry and others,
Thank you for your prompt replies.
Actually if you look at the signature of the fill method for Arrays class it clearly states that it will take two parameters the first one would be the array name and the second one would be the value that you would want to fill the array with. Also the method signature has no return type
And that is what i did.
Arrays.fill(arr1,5);

So according to the signature in java.util.Arrays, when i want to show the elements of the array it should be 55555.
@Henry, you are suggesting that i use a loop to see the elements of the array, my question for you would be that why use a loop? why not print it out straight away when the method signature does not have any return type.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
The return type is not part of the method signature. And lots of methods have no return type. They use the keyword void instead.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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.


But isn't void a return type too Campbell?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
The JLS doesn’t call it a return type.

And Ashish Dutt, please avoid coloured text; it make the post more difficult to read and there are some people who can only read certain colours.
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

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.

Thanks a lot for the welcome, Maneesh.

I do not see any place where you have printed the elements of the list.
The System.out.println(Arrays.asList(arr1)); method is being used to print the elements of the array.
Look carefully at the signature of Arrays.asList(array[]) method. What is its return type?
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.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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.
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

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.

Nope, still got no clue as to why its returning the hashed value and why its not giving the desired output. And no idea on what "backed up list " either. Googling out the term is not much help either.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14156
    
  19

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:


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Before directing you to find the answer, let me correct my statement. As Campbell rightly said, the return type is NOT a part of the method signature, but the method declaration. Only the method name and parameters expected are a part of its signature.

Now, getting back to your question, like Henry rightly said, you are simply printing the address of the integer primitive stored in the returned list when you type:



If you want to print out the elements of the list which is being returned, you would need to iterate through it. Then print each element one by one.



Building a solid base of fundamentals is one thing I always vouch for, be it any discipline. I presume you are a newcomer to the Java language. Is that correct?
Ashish Dutt
Ranch Hand

Joined: Apr 12, 2013
Posts: 172

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:


Dear Jesper de Jong,

A big thank you for clarifying the doubt so succinctly.
Your explanation made more sense than any one else's.
Yes, using the toString method prints the desired output.
Thank you again. Much appreciated. :-)
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

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.
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

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.


Agreed about the second part. It is printing the array of primitives int[] object. What does the @26f44031 part signify then Jeff?
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38881
    
  23
Doesn’t this explain the output?
Mansukhdeep Thind
Ranch Hand

Joined: Jul 27, 2010
Posts: 1157

Campbell Ritchie wrote:Doesn’t this explain the output?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Arrays.fill method not working as desired. Help would be appreciated