Skip to main content

Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 16

Danson Wachira is a certified Trainer in Computer Science, Information Technology and related studies.

Table of Contents

<< Lesson 15 | Lesson 17 >>

Lesson 16: Multi-dimensional arrays in Java

In our previous lesson about arrays in Java programming language, we have been working with arrays as a single list.

Arrays of multiple rows and columns (table), also called Multi-dimensional arrays can be implemented in Java programming language by specifying the number of rows and columns in array declaration.

This article will discuss how to implement multi-dimensional arrays in Java. Suppose we want to implement a multi-dimensional array as shown below:

Example of Multi-dimensional array (table)

^ Rows / Columns >   






















The above multi-dimensional array has 5 rows and 4 columns (not counting table header). In fact, we can refer to a multi-dimensional array as a table. To define such an array in Java, we use the following statement:

int[ ][ ] myNums = new int[5][4];

Notice that in the above statement we have two sets of square brackets. The first set of square brackets is for the rows and the second set of square brackets is for the columns. To assign values into the first row as shown in the above array, we use the following statements:

myNums[0][0] = 56;

myNums[0][1] = 82;

myNums[0][2] = 78;

myNums[0][3] = 94;

The first row becomes 0 as index positions in Java start at zero, the columns moves from 0 to 3. To assign values into the second row, we use the following statements:

myNums[1][0] = 78;

myNums[1][1] = 88;

myNums[1][2] = 55;

Scroll to Continue

myNums[1][3] = 18;

Insert all the values shown in the array and then write the following program to access and output the values. This program uses two FOR loops, one to loop through the rows and another to loop through the columns.


In the above program, counter i moves through the rows while counter j moves through the columns. The statement: System.out.print(myNums[i][j]+” , ”); output the values on a line separated by a comma. In the outer FOR loop we have the statement: System.out.println(” ”); This statement creates a new line after every row.

Notice that in the above example, the output has zero values in rows 3, 4 and 5 because I did not assign any value for those rows in the program. Once you assign values in your program you’ll be able to see the actual values being displayed as outputs.

Here is another simple example to produce a multiplication table of 10x10. Write a new program and try it.


My suggestion here is that you try more examples on arrays as arrays are integral aspect of any programming language. In the next lesson, we shall look at how we can implement and manipulate arrays of string type.

<< Lesson 15 | Lesson 17 >>

Want to make make online?


Danson Wachira (author) from Nairobi, Kenya on August 30, 2012:

@Cheryl J., Glad to see you here, programming is among us and we use programs every day. It doesn't hurt to know a bit of programming in today world. Thanks for the visit and comment.

Danson Wachira (author) from Nairobi, Kenya on August 30, 2012:

@TToombs08, thanks for stopping by and commenting, am glad you found this article to be useful.

Cheryl J. from Houston, TX on August 24, 2012:

Very interesting information on programming. I will practice your concepts. A great hub and charts.

Terrye Toombs from Somewhere between Heaven and Hell without a road map. on August 24, 2012:

A very interesting hub on programming in Java, dwachira. I hope I never have to, but if I find the need or desire, I will be sure to refer to your very helpful and thorough instructions.

Related Articles