Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
Java supports the use of multidimensional arrays. Just as with simple arrays a multidimensional array contains items all of the same data type. And once the sizes of the dimensions of the array have been set you can't change them at runtime. I'll show you how to use a multidimensional array in this empty project 2DArrays. I'll start by declaring the array. This is going to be a multidimensional array of strings, so I'll start with the data type followed by brackets, but because this is going to be a two-dimensional array, I add another pair of brackets.
Next, I assign the array name, I'll call it states. Then after the equals assignment operator, you put in the new keyword, once again you put in the data type. And now just as with simple arrays you declare the array size, but now you're declaring both the top-level dimension and the child dimension. I'll set the size of my top-level dimension at three and my child dimension at 2. Now my goal is to create a primary array of three items each one representing a state. And then within the secondary array or child array there'll be two items, one for the name of the state and one for its capitol.
Now I'll set the values. Use similar syntax to set values. I'll start with states, bracket, 0, bracket, 0. The first item in the first primary array. And I'll set that to a value of California. Next I'll create the second item in the first primary array using the syntax states, bracket, 0, bracket, 1, and I'll set that to the name of the capitol of California, Sacramento. Now I'm going to copy and paste those two lines of code a couple of times.
I'll create two new copies. And for the second pair, I'll change the primary array item to one. And for the third pair I'll set the primary array item to two. Now I'll set the values, the second state will be Oregon, and its capitol is Salem. And for the third pair I'll use Washington and Olympia. Now to output these values, I am going to use a loop inside a loop.
I'll use two for loops and I'll manage my output by using a StringBuilder object to build strings for each combination of state name and capitol. I'll start by typing the word for and pressing control space and I'll choose for, iterate over array. In the primary for loop the integer counter variable will be named i. And I'll be looping through the primary array, this syntax is the same as for a simple array. I'm iterating through the primary dimension, the one that has three items.
Then I'll click into the primary for loop and I'll add a secondary for loop. I'll once again type for and choose iterate over array. And this time, Eclipse knows that I've already used the variable i, so it uses the variable j for the secondary counter. Now to make sure that I'm iterating over the secondary array, I'll place the cursor after name of the array, states, and I'll add bracket, i, bracket. So now I have an outer loop and an inner loop.
Now my goal is each time through the loop, I'm going to output the state name and its capitol all in a single line. To put it together I'll declare a StringBuilder. I'll put the declaration and initialization of the StringBuilder in the outer loop, so then I can manipulate and append to it in the inner loop. So I'll set the data type to StringBuilder and its variable name to sb and I'll initialize it. Within the for loop, I'm simply going to add the values to the StringBuilder using sb.append and I'll output states, open bracket, i, open bracket, j.
And then after the for loop I'll use a println command to output the StringBuilder to the console System.out.println. And I'll just refer directly to the StringBuilder object. I'm not quite done yet but let's see the results so far. I'll save and run the application and I'm seeing that I am successfully getting both the state name and its capitol output to the same line. So now I need to add a little bit of conditional logic so I can separate those values.
I'll go to the inner loop and I'll add some conditional code. I'll type if and press control space and choose the if statement and I'll set the condition to if J has a value of zero. So if I'm working on the state name then j will have a value of zero. And before I append the state name, I want to append a little bit of text. So I'll say sb.append, I'm appending a string to the existing StringBuilder, and so now the text should say the capitol of and then the name of the state. Let's check that.
The capitol of California, so now, all I have to do is separate those two values and I'll use an 'else' clause for that. If I'm just about to append the capitol name to the string builder, I'll put in a space the word is and another space and let's see how I'm doing. And now I have sensible output, the capitol California is Sacramento, the capitol of Oregon is Salem and so on.
So I have successfully organized my data into a multidimensional array. Now the strength of this approach is that if you know exactly how much data you're going to be working with, it's a pretty straightforward process to store it in memory and then access it at runtime. If on the other hand you have to be able to expand the array or shrink it at runtime, because you don't know how much data you're going to be dealing with and you can't just declare the array, and then deal with a fixed size, then you should start looking at the array list class instead.
And I'll talk about that in another video.
Get unlimited access to all courses for just $25/month.Become a member
61 Video lessons · 92470 Viewers
82 Video lessons · 104137 Viewers
71 Video lessons · 75603 Viewers
56 Video lessons · 106976 Viewers
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.
Your file was successfully uploaded.