how to print a 2d array in java This is a topic that many people are looking for. bluevelvetrestaurant.com is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, bluevelvetrestaurant.com would like to introduce to you Multi Dimensional Printing – Java Programming Tutorial #29 (PC / Mac 2015). Following along are instructions in the video below:
We want to see how we can print out these multi dimensional arrays and i im not talking about printing out one value because we know how to do if wanna print out this value or this value. We know exactly how we do that first box is for the rows in the second boxes for the columns you just indicate inside of there. What you want to print out.
But if we want to print out this entire array. Im talking about the entire thing and i want this to look nice. I want to look something like this one two three four five six and then seven eight nine.
We have our different rows and our different columns row. 1. Which is over here row 2.
I guess i say this is row. 0. Right this is the first row which corresponds to this over here.
Which is 0. This is row 1. Which is the second row.
Which corresponds to this and you have your third row. Which corresponds that now this is gonna be a bit confusing right so this is one of the videos. Were gonna move really slow to try to make sure we know exactly whats going on im not putting a lot of markup in here.
Just so we know where everything starts and finishes. Because this can be curly braces all over the place so im gonna restart of class up here. Im gonna go ahead and write start of maine.
Over here. Im gonna go ahead. And write end of maine.
Over here. And well put end of class over here just so we know whats going on you dont necessarily need to do this in your program. If youre writing this along with me.
However for watching it on screen. I recommend we do it so one go ahead and design some function right were gonna go to find a methods im gonna design this outside of the main but inside of the class and this is gonna be public static void. Im gonna call this print array so this method is gonna be for printing an array now we dont know which array we want to pass through here just yet so that means we need to use an argument to leave this open to not open to interpretation to open to be flexible right it needs to be able to have any array passed through it doesnt know exactly what the array is gonna be or how big its gonna be so we need to really design this generally so inside of here.
My argument is gonna be passing through a a now a is gonna be the argument now we know if i just wrote integer a well that means. Its expecting a variable. If i just wrote integer.
A and open and close square brace. That means expecting a one dimensional array and if you put two boxes that means youre expecting a multi dimensional array. So you can have one slot open for your rows and one slot open for your columns.
But youre not gonna put anything inside of here this just indicates that you want to pass through an array now were gonna go ahead and open up. Here so im gonna go ahead and make some more markup. So im gonna write start of print array method and down here.
Im gonna go and the printer. A method and we have a lot more curly braces coming dont worry now we saw when we wanted to print out our one dimensional race. We did that using a for loop and the for loop.
It allowed us to keep going through the array and keep going through one value at a time and then printing them out now. Were gonna have to have two for loops when were dealing with a multi dimensional. Array.
And this is gonna get a little bit confusing so start off inside of this print array function that we created. Were gonna write the keyword forward to indicate we want to work with a for loop. Now this first one is gonna be keeping track of the rows.
So this here is this counting variable. We always have inside of the brackets of a for loop. Were just gonna go ahead and call this row right keep it nice and simple and ill start up by signing that a value of zero.
Because we know that if were dealing with arrays arrays like to start counting at zero. Just like computers. So we should go ahead and respect that and make that at zero as well it puts semicolon over here and now we want to figure out how what type of condition.
Do we want to have over here. We know that we want to have row is going to be less than what exactly does it need to be less. I want to think about this here right we know that whatever array.
Were gonna be passing through is gonna be represented by a so we should use a over here because we want this to be flexible to be able to accommodate for whatever array. We pass through so were gonna use this a that we have over here for our argument. And we want to make sure that this is gonna be the size of the array.
We saw that weve used the dot length keyword over here this means. Its gonna take the size of the array now im going to show you guys passed you guys through the process after we begin you know actually going ahead and passing are not prius multi dimensional array through here. But this will essentially be not prius dot length.
Once you actually go ahead and pass the array through so keep that in mind. This is gonna be replace by the size of the array or the length next. Were gonna go ahead and were going to put our increment operator.
Which is just gonna be row plus plus. Which means each time we pass through this loop. We want to go ahead and increase the row by one thats all we need for our conditions.
And thats gonna be our for loop. So im gonna wrote start of row for a loop and down here. Im gonna write end of row for loop.
Now we have more curly braces coming this is why im putting all this notation here because its getting kind of confusing now were not gonna really be writing anything inside of our row for a little because we can go ahead and put our second for loop inside of it and this is gonna be the for loop for the column. So were gonna have the same thing. Ill have this counting variable called column.
Its gonna keep track of the columns. Now were gonna see what exactly do we want to put here as our condition. Were gonna put call him and were gonna put less than but what is it gonna be less than its gonna be less than a which is gonna be the array you pass through and then we passing through row.
So whatever variable youre on over here and dont worry were gonna see the whole process of passing through as we progress. Were gonna pass this row variable so whatever row variable. Were on at that time is gonna be inside of the array and now were gonna put dot length.
So what exactly is the purpose of this now if we go back up here right. When i say wrote a length right im talking about a row length right so im talking about here. It sees that this row is being 1 2.
3. Thats the length of the row. Its only gonna have 3 passes through the loop right and then its gonna stop as soon as it reaches the end here.
Obviously. This will be 0 1. 2.
And then it stops now inside of here you want to actually accommodate each row. Because its possible that you might not have the same amount of values on each row. Right you might have less on a certain row.
So if i were to go ahead and write a which means i want to talk about whatever array. Were gonna be passing through. Im gonna be talking about whatever row.
Were in because the first box here is always gonna indicate the row. So now whatever row you are in because of here right this is going to be counting your row. Its gonna indicate which row.
You want and then you want the length of that row. Which is either going to be in this case. Itll be 2 for each of them.
Which is 0 1.
2 or 3. I guess you can say it also be 3. I should what should one with 3 ready.
This is 3. Its not to me youre counting at 2. But not for the sake of this however you always you know you have to keep in track.
You might have a different length of a row just keep that in mind. And now were gonna have our increment. So column plus plus and now we have to put our curly.
Braces and thats a lot of curly braces right so this is start of column for a loop. I should go ahead and go to this here can i go to this yeah. Give me some space can you leave no okay.
Thats not leaving entirely. But this is this is fine. This is nice and down here.
Im gonna go ahead. And write end of column for loop all right now that was an ordeal now what are we gonna want to put inside of this one right because you have to put something inside of these loops it would be boring. If we just left them blank.
What im gonna go ahead and put. And help but im gonna write systemoutprint not print line print youll see why im doing this in a moment and now inside of here. Im gonna go ahead and put.
8. Which is going to represent the array and passing through. Im gonna put ro inside of the first box.
We know that thats gonna represent a row and im gonna call them inside the second box and thats gonna represent call him now im also gonna go ahead and put a space here. Im gonna put a plus and im just gonna put a space inside of two quotation marks right here and youll see why i did that in a moment just like in space out the numbers now we know that if we look at this here. You know the first time you pass through you can have zero here and youre gonna have zero here well.
We know thats gonna correspond to the one over here right. And then youre gonna go ahead and pass through the column loop. Because it knows that its time to go through the column loop.
So gonna go pass through the tire loop and youre gonna go through each column inside of row zero. We know. This is a column zero in row.
0. This is mean column 1. In row.
0. And this is gonna be call them two in row 0. So its gonna pass through and print out that line and once youre finished with that line its gonna finish the loop.
And then youre gonna have to go back into the row for loop. And now youre gonna be in row 1. So then i get a pass through and youre gonna go through row.
1 column. 0 row. 1 column.
1 and then row 1 column 2. Now you finish. The column loop.
Oh oops you have to go back inside the for loop. One more time because you have not satisfied a dot length. Yet and now youre gonna have row.
2 column. 0. Which is gonna be the 7.
Then youre gonna have row. 2 column 1 and then row. 2 column 2.
And now youre finished with your thing. And thats why we have this little thing here this little space just to give us a little bit of space. So it looks nice and neat.
Now were almost done were done with this part over here right and this is extremely complex you know i this this is this is being brave here. We do need to include one more thing right because we want to make sure that we actually have the space or else all this stuffs gonna be on the same line so where are we gonna go ahead and put a request to print line right the pin line is going to allow us to get that new space. So if we put a system output.
And we just write print line and we dont put anything inside the brackets. Its gonna give us that new line that we want so. Where exactly should we put it.
Like i shouldnt put it inside of here. Theyre put inside of here then everything would be on a different line. I want all of the rows to be on different lines.
So whenever we go to a new row right over here before we begin going to that row. I want to go ahead and make a new line just so we go ahead and make it nice and neat whoops yeah nothing inside there sorry im clumsy right now there we go so now we have the print line right. There.
Which means. Were gonna have everything on different lines. Well now we still need to go ahead into our main and we need to go ahead and indicate that we want it to use i want to use this method that we created this print array method and we need to tell exactly which array.
We want to pass through so ill go inside of our main up year. And im gonna go ahead. And write the name of the array write something go ahead and see that the array that i want to use is called the where is it where is it print array sorry about that the method.
I want to use this to print. Array and i want to call it usually not prius array now you dont have to go ahead and put boxes inside of here right because remember this is all being replaced by a so. If you go ahead and replace.
Just the a by not prius. Well you already have those two boxes. Awaiting for you right here.
So it already assumes that it can be a multi dimensional array. You dont need to do anything about that so go ahead and leave that right there and now we can go ahead and make this nice. If you really want it to actually dont need to do this you know what no no im not even bothers gonna pick a nice little statement to say this is the mode.
This is your array. But we can go ahead and run this actually see that it works we go ahead and run run run run run and boom. There is our array look at that.
Its beautiful 1 2. 3. 4.
5. 6. 7.
8. 9. Thats how we make we print out the table of our array right were printing out the array in the actual way that it should appear and i think that is pretty awesome so if you guys enjoyed this video make sure you you know watch more of the videos.
I guess and if you have any questions leave in the comments below and i will respond to you as soon as possible thank you very much for watching i know this was confusing. But if you have any questions or need some clarification comments below. I will help you good bye.
Thank you for watching all the articles on the topic Multi Dimensional Printing – Java Programming Tutorial #29 (PC / Mac 2015). All shares of bluevelvetrestaurant.com are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.