Programming in Java Netbeans - A Step by Step Tutorial for Beginners: Lesson 16
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;
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 = 56;
myNums = 82;
myNums = 78;
myNums = 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 = 78;
myNums = 88;
myNums = 55;
myNums = 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.
- Programming In Java - A Step By Step Tutorial For Beginners: Lesson 6
In this lesson we’ll learn about another useful class for accepting user inputs and displaying results. This class is called JOptionPane class and is located in the javax.swing library.
- Programming In Java - A Step By Step Tutorial For Beginners: Lesson 9
In this lesson we shall learn how to use the Switch statement in Java programming. Switch statement is a selection statement, that means when used, it select one value among many values. Switch statement can also be used instead of IF .. ELSE ...
- Programming In Java - A Step By Step Tutorial For Beginners: Lesson 10
The FOR loop is one of the most common looping controls used in Java. The FOR loop forces the program to repeat a statement or a group of statements a specified number of times. It has three parts; the initial value part, condition part, and ...
- Programming In Java - A Step By Step Tutorial For Beginners: Lesson 11
The WHILE loop in Java execute a statement or a group of statements so long as the specified condition remains TRUE. The loop starts with the keyword “while”. Just after the keyword you open the round brackets and specify the condition to be tested.