Two-Dimensional Arrays in Java
A Simple Thought
Imagine a classroom where students sit in rows and columns. Each seat has both a row number and a column number. If you wanted to record where every student sits, you would need a way to store information that has two positions, one for the row and one for the column.
That is exactly what a two-dimensional array does. It lets the computer store data in a table-like form.
In this lesson, you will learn how to use two-dimensional arrays to store and display data in rows and columns.
The Project: Marks Table
You will create a program that stores the marks of students in three subjects.
Each student will have a row, and each subject will have a column.
The program will print the table neatly on the screen.
Your output should look like this:
1 2 3 4 5 6======================== STUDENT MARKS TABLE Student 1: 78 82 90 Student 2: 85 88 92 Student 3: 80 75 84 ========================
You will use a two-dimensional array to store the marks and nested loops to display them.
The Concept: Two-Dimensional Arrays
A two-dimensional array is like a grid or a table.
It has rows and columns, and each cell can store a value.
Here’s how you declare one in Java:
1 2 3 4 5int[][] marks = { {78, 82, 90}, {85, 88, 92}, {80, 75, 84} };
In this array:
- Each inner set of numbers represents a row (one student).
- Each number in the row represents a column (one subject).
- The first index represents the row, and the second represents the column.
For example, marks[1][2] means the second student’s third subject.
Why This Matters
Two-dimensional arrays are used whenever you need to organize information in rows and columns.
They are found in many real-world situations:
- Tables of student marks
- Spreadsheets and scoreboards
- Board games and puzzles
- Image data stored as grids of pixels
They help you manage complex data in a simple and structured way.
Let’s Build the Marks Table
Here’s the complete program:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23public class StudentTable { public static void main(String[] args) { int[][] marks = { {78, 82, 90}, {85, 88, 92}, {80, 75, 84} }; System.out.println("========================"); System.out.println(" STUDENT MARKS TABLE"); for (int i = 0; i < marks.length; i++) { System.out.print(" Student " + (i + 1) + ": "); for (int j = 0; j < marks[i].length; j++) { System.out.print(marks[i][j] + " "); } System.out.println(); } System.out.println("========================"); } }
When you run this program, it prints each student’s marks in a clean table format.
The outer loop goes through each student, and the inner loop prints their marks for each subject.
How It Works
- The array marks stores data for three students and three subjects.
- The outer loop runs once for each row, representing a student.
- The inner loop runs for each column, representing the student’s marks in different subjects.
- The System.out.print() statement prints each mark on the same line.
- System.out.println() moves to the next line after each student’s data is printed.
This combination of nested loops and two-dimensional arrays gives your program the power to handle table-style data.
Learn Together with AI
If you are using an AI Copilot, you can describe the table you want to create and it will generate the code for you.
Try this prompt:
“Write a Java program that stores student marks in a two-dimensional array and prints them in a table.”
After it works, try asking for variations like:
- “Add a total for each student at the end of their row.”
- “Show the average marks per subject.”
- “Format the table with better spacing.”
Each change will help you understand how to use nested loops to work with rows and columns.
Practice Challenge
Create a program that stores the scores of four players in three games.
Then print the scores in a table format like this:
1 2 3 4 5 6 7======================== GAME SCORES TABLE Player 1: 12 8 15 Player 2: 10 9 13 Player 3: 14 11 16 Player 4: 9 7 10 ========================
Use a two-dimensional array and nested loops to print each player’s scores.
Example Solution
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24public class GameScores { public static void main(String[] args) { int[][] scores = { {12, 8, 15}, {10, 9, 13}, {14, 11, 16}, {9, 7, 10} }; System.out.println("========================"); System.out.println(" GAME SCORES TABLE"); for (int i = 0; i < scores.length; i++) { System.out.print(" Player " + (i + 1) + ": "); for (int j = 0; j < scores[i].length; j++) { System.out.print(scores[i][j] + " "); } System.out.println(); } System.out.println("========================"); } }
When you run this program, it prints a clean table of game scores.
You can easily add more players or more games by changing the array’s size, and the loops will adjust automatically.
Coming Up Next
You’ve now learned how to organize and display data in tables using two-dimensional arrays.
Next, you will take another important step by learning how to create your own methods in Java, small reusable blocks of code that make your programs more organized and powerful.