Spiral Matrix in Java | Spiral of Numbers in Java | Matrix in Spiral Form

Steps for Creating a Spiral Matrix in Java:

  • First of all, define the size of the array. If you want to ask user, get the size of array by using any method (Scanner class, Command line argument, Buffered reader class).
  • Now get the minimum and maximum number of row and column of the matrix.
  • In order to generate a spiral matrix, you have to take care of some steps,
    • Step - 1. Print the first row of the matrix from Left to right.
    • Step - 2. Print the last column of the matrix from Top to Bottom.
    • Step - 3. Print the last row of the matrix from Right to Left.
    • Step - 4. Print the first column of the matrix from Bottom to Top.
    • Step - 5. Decrement the number of maximum Row and Maximum Column, and Increment the number of minimum Row and Minimum Column by 1 as they already get printed.

Code for Spiral Matrix in Java:

package com.missTechy; import java.util.Scanner; public class SpiralMatrix { public static void main(String[] args) { // Ask the user for matrix size. // It is a square matrix that's why this code only asks for a single value. System.out.println("Enter the matrix size :: "); // Create an instance of Scanner class present in util package. // Object of scanner class is used to take input from user. Scanner scanner = new Scanner(System.in); // Normally when scanner class object is used then it get the value in String format. // scanner object calls nextInt() method which is used to convert String into Int. int size = scanner.nextInt(); // Create a 2D array of size n*n named spiral. int[][] spiral = new int[size][size]; // Starting value of array. int value = 1; // number of minimum column in the matrix int minCol = 0; // number of maximum column in the matrix. int maxCol = size - 1; // number of minimum rows in the matrix. int minRow = 0; // number of maximum rows in the matrix. int maxRow = size - 1; // iterate till value is less than the total number of values matrix can hold. while (value <= size * size) { // values for the first row. // from column 0 to the last column. for (int i = minCol; i <= maxCol; i++) { // by fixing the matrix first subscript to minRow we are setting the value only for first row. spiral[minRow][i] = value; // increment the value. value++; } // values for the last column. // from first row to last row with the fixed last column. for (int i = minRow + 1; i <= maxRow; i++) { spiral[i][maxCol] = value; value++; } // values for the last row. // form last column to the first column for (int i = maxCol - 1; i >= minCol; i--) { spiral[maxRow][i] = value; value++; } // values for the first column. // from last row to the first row. for (int i = maxRow - 1; i >= minRow + 1; i--) { spiral[i][minCol] = value; value++; } // change the matrix min column from 0 to 1. minCol++; // change the minimum row from 0 to 1. minRow++; // change the maximum number of column -- a less than from maximum. maxCol--; // change the maximum number of rows -- a less than from maximum. maxRow--; } // print the spiral matrix. for (int i = 0; i < spiral.length; i++) { for (int j = 0; j < spiral.length; j++) { System.out.print(spiral[i][j] + "\t"); } System.out.println(); } } }

Output of Spiral Matrix in Java:

Post a Comment

4 Comments