• Post Reply Bookmark Topic Watch Topic
  • New Topic

Arrays.fill and printing question  RSS feed

 
Brian King
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I'm practicing arrays and am unsure about something.  In the following code, I don't understand why when I run it the array is showing 0, 1, 2 , 3, etc. rather than 2, 2, 2, 2.  I also don't
understand why it's printing out, or what it is for that matter, [I@15db9742 at the end. 

Thanks for any advice.

Brian



What prints out:
 
Norm Radder
Rancher
Posts: 2240
28
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
why it's printing out, or what it is for that matter, [I@15db9742 at the end.  

[I@15db9742 is the String returned by the default toString method for arrays.
Use the Arrays class's toString to format an array for printing:

  showing 0, 1, 2 , 3, etc.

That looks like the loop index that is being printed, not the contents of the array.  Use array notation to access the elements in the array:  theArrayName[index]
 
Brian King
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks so much!  When I do like you said, it shows up how I was imagining it.  I'm lost about another thing though if you happen to have the time. 

This is what works after reading your post:



What I don't understand is why if I do the following it prints out twenty rows.  I thought that wasn't possible with a one dimensional array?  Is the for loop
saying to print out the same thing twenty times rather than one line of twenty?  I don't really see the difference between the above code and below
that would lead to that.



prints out:

 
Norm Radder
Rancher
Posts: 2240
28
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
the following it prints out twenty rows.

That is done by the for loop at line 11.  It loops for the length of the array (20) times.
 
Patrick Balleux
Greenhorn
Posts: 4
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

The main reason for printing  -0,1,2,3,4... is that you are printing "j" and not the content of the array:  numbers[j]



There many ways to do this operation but I wonder what you are trying do?

Have a great day!


 
Patrick Balleux
Greenhorn
Posts: 4
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Darn, made a typo.

LINE 12 ==> if (j == numbers,length)

Patrick Balleux wrote:Hi,



The main reason for printing  -0,1,2,3,4... is that you are printing "j" and not the content of the array:  numbers[j]



There many ways to do this operation but I wonder what you are trying do?

Have a great day!


 
Knute Snortum
Sheriff
Posts: 4281
127
Chrome Eclipse IDE Java Postgres Database VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Still a problem, I'm afraid: Also, the true clause will never execute because j is never equal to numbers.length.
 
Brian King
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks everyone.  I'll mess around when I get home tonight. I'm not really trying to accomplish anything but more just dabbling.
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!