• Post Reply Bookmark Topic Watch Topic
  • New Topic

tutorial on printing arrays?  RSS feed

 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Does anyone know of a good webpage showing how to print out arrays (with examples)?

Thanks.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To print out an array all you need is an iterating statement and an output statement combined

You can start with these three basic ways:

1. The for loop



2. The enhanced for loop



3. The while loop

 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
4. Using a method that converts an array to String:
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks. I guess I should give an example of what I mean also. Right now I'm doing a program where I roll two die five times each,
though I have problems printing out arrays no matter what I'm working on. I had the same problem as the question evolved on
this thread: http://www.coderanch.com/t/632311/java/java/arrays. I've spent a lotof time searching webpages, reading books,
and I can't seem to find any information that explains it, including with examples. For example, when you make an array but then
use JOptionpane for the user to select a number, I don't understand how you print that out. I have no idea how I would even begin
to do the below as a multidimensional array and then print out the five results for each die. So, it's not this problem but more just
getting an understanding of the syntax of bringing it all together. I've tried using the for and while loops but don't know what actually goes in them.

Here is my class:



This is the main:

 
Piet Souris
Master Rancher
Posts: 2044
75
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hi Ryan,

the easiest way is to give your Die class a method "toString()", for instance:


But since you are having 5 dice pairs, it might be wise to give each instance of the Die class some id, and include that
in the toString method.

Greetz,
Piet
 
Ryan Bishop
Ranch Hand
Posts: 143
5
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you.
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!