• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
 
Ashish Dutt
Ranch Hand
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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:
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 172
Python MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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. :-)
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Doesn’t this explain the output?
 
Mansukhdeep Thind
Ranch Hand
Posts: 1164
Eclipse IDE Firefox Browser Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Doesn’t this explain the output?

 
reply
    Bookmark Topic Watch Topic
  • New Topic