Join Peggy Fisher for an in-depth discussion in this video Working with two-dimenional arrays, part of Learning Java 8.
- There are times in programming where you can't use just a single dimensional array. For example, how would you create an array that holds both a list of original prices and a list of discounts in the same array? We could use two parallel arrays but a better way might be to create a two dimensional array. If you've ever worked with an Excel spreadsheet a 2D array is very similar. In Excel, the columns are labeled A, B, C, D, etc. and the rows are numbered 1, 2, 3, etc.
In Java we use the Index position to reference each cell. So, instead of A1 for the very first cell in Excel we use  to indicate the row 0, column 0, for the first element in our two dimensional array. Let's start with a syntax for a 2D array in Java. In this program, I'm gonna create a two dimension array of prices where the original price is in the first column and the discounted price is in the second column.
So, I'm gonna use a double. When we create a one dimensional array we have one set of . But since this array has two dimensions, I need two sets. I'm gonna call it prices. I'm gonna set it equal to a new_double array. Now, as we mentioned before you have to give the size of the array. Since a two dimensional array has both rows and columns I need to provide the size of each. So, in my case, I'm gonna put 5 for the number of rows and next to it I'll put 2 for the number of columns.
The first column is the original price. Second column is the discounted price. Okay, let's use this new array to store 5 prices in the first column and then calculate the discounted amount. We'll ask the user to enter the original price and then calculate the discount. We will apply a thirty percent discount. Since I know I wanna ask the user for five different values I'm gonna go ahead and add a for loop. Inside my for loop I wanna ask the user for the original price. I wanna read that value into my array.
So, I'm gonna use prices[i] to represent the row. Now, remember which column had the original price in it. It was the first column but anytime we're dealing with arrays the first column always has a value of 0, just like the first row. So, prices[i] is equal to in.nextDouble and I just realized I forgot to import my java.util and I wanna add a scanner object.
When I come back up, I'll enter the next original price, etc., but why don't we go ahead and calculate the discounted price for the second column while we're here? I can say prices[i]. This represents that second column. Is equal to whatever the price was in the first column which was in position 0, row I, position 0, times, I wanna show the discounted price and I'm gonna give a thirty percent discount.
So, if I take the original price and I multiply it by .70 that will give me my discounted price of thirty percent off. Okay, once this runs my prices array will have all the values in both columns that I need. So, let's print it out and make sure it works. Again, I use a for loop and I'm gonna print out System.out.printline and I'm going to say original price and here I'll include a $, and then I'll do a + and I'll use the prices array at position I, at position 0, row I, column 0.
In the same line, I'm gonna go ahead and put the discounted price. I'm gonna use an escape sequence which is a /t which will automatically tab over. So, it gives me some nice spacing. So, discounted price. Again, I'll put my $, "+prices[i]. Okay, this should print all five items of the original price and the discounted price. Let's go ahead and give this program a try.
Okay, I know the original price. I'll make them easy so I can figure it out in my head. 10 dollars, 20 dollars, 30 dollars, 40 dollars, and 50 dollars. Okay, I'm gonna make my output screen bigger so I can see all the output. It looks like when the original price as $10 thirty percent off would make it 7. If it was $20, it's 14. $30, it's 21. $40 is 28, and $50 is 35.
It looks like it worked. So, this was an example of a two dimensional array. Usually we don't go any bigger than a two dimensional array although you could have a 3D array as well. If you haven't given this a try yet, go ahead and create a two dimensional array.
- Downloading and exploring NetBeans
- Understanding Java basics: data types, strings, arrays, and more
- Controlling flow with functions and loops
- Creating classes
- Sorting and searching arrays
- Manipulating files
- Handling errors
- Building GUIs