Java: Multidimensional Arrays

An error occurred trying to load this video.

Try refreshing the page, or contact customer support.

Coming up next: Java: Dynamic Arrays

You're on a roll. Keep up the good work!

Take Quiz Watch Next Lesson
Your next lesson will play in 10 seconds
  • 0:04 Multidimensional Arrays
  • 0:51 Creating…
  • 1:42 Populating…
  • 2:13 Processing Arrays
  • 3:39 3D Arrays
  • 4:05 Lesson Summary
Save Save Save

Want to watch this again later?

Log in or sign up to add this lesson to a Custom Course.

Log in or Sign up

Speed Speed

Recommended Lessons and Courses for You

Lesson Transcript
Instructor: Martin Gibbs

Martin has 16 years experience in Human Resources Information Systems and has a PhD in Information Technology Management. He is an adjunct professor of computer science and computer programming.

We are entering multiple dimensions: multiple dimensions in Java arrays, that is. A multidimensional array is a nested array; an array within an array.

Multidimensional Arrays

We are entering another dimension. Possibly many. An array is a bucket. It's an object that holds a set of values of a consistent type. Int, char, double, etc. Think of a one-dimensional array as a list of values, which are indexed; that is, can be referenced by their location in the list. The first element in the list starts with 0 and increments to the last one.

A multidimensional array in Java is really an array within an array, and as more dimensions are added, the hall of mirrors continues. For purposes of sanity and practicality, we'll cover mainly two-dimensional arrays and briefly show code for a three-dimensional array.

We can imagine a two-dimensional array (2D) as a table of values or a matrix. There are rows and columns within the table or matrix.

Creating Multidimensional Arrays

The basic steps for creating multidimensional arrays are:

  1. Declare the array
  2. Allocate space for the multidimensional array

To declare an array, you need to tell it what type it is. Think of declaring a variable; it's the same concept. You need to tell Java if it's dealing with strings, numbers, or objects.

The syntax for creating a two-dimensional array is:

data type [] [] arrayName;

Let's say we have a simple table of integer values; it would be declared thus:

int [] [] table;

Now, to create a table that is 5 by 5, use the following:

table = new int[5][5];

Array Example
Array Example - Empty

Remember that Java starts counting at 0. Therefore, the first row/column box is 0,0; if we set row 3, column 4 to 245, the value would look like:

Array - Value in Cell
Array - Single Value

Simply creating the multidimensional array doesn't actually do anything. Let's look at how data actually gets into the array.

Populating Multidimensional Arrays

For our array of int values, we can provide the data needed as follows:

int [][] table = {

To access the components of a two-dimensional array (a table or matrix), use the following syntax:

array_name [row] [column];

Within the brackets, the row is the row index, or position of the row (remember everything starts at 0). The column is the column index. In the previous examples, we set row 4, column 3 to a value of 245. In Java that code looks like:


Where table is the name of our array or matrix or table.

Processing Arrays

Now that we've created the table or matrix and figured out how to reference data elements, how do you work with the data? What's a real-world example? Think of a GPS application that finds the closest restaurant to where you are. Although the programming is complex, the concept is simple: find the closest set of coordinates to the user's location. Coordinates can be saved into a two-dimensional array, because we have an x and y value for each point on the map.

Plot Example
Plot Example

The user is at 0,0, and the table or matrix of values is:

X and Y values example
X and Y values example

The closest point to the user is 3,2. In Java, the following code displays a simple solution for finding the closest point. Processing a two-dimensional array in Java requires two for loops: one to walk through the rows, the other for the columns.

To unlock this lesson you must be a Member.
Create your account

Register to view this lesson

Are you a student or a teacher?

Unlock Your Education

See for yourself why 30 million people use

Become a member and start learning now.
Become a Member  Back
What teachers are saying about
Try it risk-free for 30 days

Earning College Credit

Did you know… We have over 200 college courses that prepare you to earn credit by exam that is accepted by over 1,500 colleges and universities. You can test out of the first two years of college and save thousands off your degree. Anyone can earn credit-by-exam regardless of age or education level.

To learn more, visit our Earning Credit Page

Transferring credit to the school of your choice

Not sure what college you want to attend yet? has thousands of articles about every imaginable degree, area of study and career path that can help you find the school that's right for you.

Create an account to start this course today
Try it risk-free for 30 days!
Create an account