2-D Arrays Part 1 – Intro to Java Programming

You’ve seen how arrays can store a sequence of values such as this one. Here we have three prices for three different kinds of gasoline. Well, what if we have two gas stations in this case you’ll want to store a two dimensional arrangement with rows and columns, two dimensional arrays can do that. In this example we would have three rows and two columns. In Java we get such a arrangement by saying, give me a new array of doubles, of floating point numbers and I want three rows, two columns. In general, the first number is the number of rows, the second one the number of columns. Note the type, it’s a double brackets brackets meaning, it’s a two dimensional array, of numbers. If you already know which numbers should go into the two dimensional array, then you can supply them like this. Again, here I have a two dimensional array, of prices. And now, you put a, pair of braces, for the entire array, and then for each row you put another pair of braces with the values. So here we have three rows, each of which is enclosed in a pair of braces. Now you know how to declare a two-dimensional array. Next let’s have a look at how we can access its elements.

Leave a Reply

Your email address will not be published. Required fields are marked *