Understanding and Exploring a Simple Java Program for a Two-Dimensional Array

Introduction

In this Java program, we explore the creation and handling of a two-dimensional array. The program prompts the user to input the dimensions (number of rows and columns) for the array and then populates and displays the array accordingly. Let’s analyze the code step by step.

TwoDArray Class

import java.util.Scanner;

public class TwoDArray {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);

        // Input for the dimensions of the two-dimensional array
        System.out.println("Enter the number of rows:");
        int a = s.nextInt();
        System.out.println("Enter the number of columns:");
        int b = s.nextInt();
        int arr[][] = new int[a][b];

        // Input for the two-dimensional array
        System.out.println("Enter numbers for the two-dimensional array:");

        for (int j = 0; j < b; j++)
            for (int i = 0; i < a; i++) {
                arr[i][j] = s.nextInt();
            }

        // Display the elements of the two-dimensional array
        System.out.println("Printing the two-dimensional array:");

        for (int j = 0; j < b; j++)
            for (int i = 0; i < a; i++) {
                System.out.println(arr[i][j]);
            }
    }
}

Program Explanation

Importing Necessary Libraries

import java.util.Scanner;

We import the Scanner class from the java.util package to allow us to take user input.

Main TwoDArray Class and Method

public class TwoDArray {
    public static void main(String[] args) {
        // Program logic goes here
    }
}

This is the main class and method of our program, where the execution begins. Inside this method, we will write the logic for taking user input for the two-dimensional array and then displaying the stored data.

Input and Storage of Two-Dimensional Array Data

// Input for the dimensions of the two-dimensional array
int a = s.nextInt();
int b = s.nextInt();
int arr[][] = new int[a][b];

// Input for the two-dimensional array
System.out.println("Enter numbers for the two-dimensional array:");

for (int j = 0; j < b; j++)
    for (int i = 0; i < a; i++) {
        arr[i][j] = s.nextInt();
    }

We prompt the user to input the number of rows and columns for the two-dimensional array. Then, we create the array based on these dimensions and prompt the user to input the elements for the array.

Displaying Two-Dimensional Array Data

// Display the elements of the two-dimensional array
System.out.println("Printing the two-dimensional array:");

for (int j = 0; j < b; j++)
    for (int i = 0; i < a; i++) {
        System.out.println(arr[i][j]);
    }

Finally, we display the elements of the two-dimensional array to the console.

Conclusion

In this program, we explored a Java program that demonstrates how to create and work with a two-dimensional array. Understanding this basic program is a crucial step toward handling multidimensional data structures in more complex applications. Happy coding!

Leave a Comment

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

Scroll to Top