Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6713647
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T08:24:18+00:00 2026-05-26T08:24:18+00:00

Possible Duplicate: Pattern consisting of numbers moving in clockwise direction around a rectangular shape

  • 0

Possible Duplicate:
Pattern consisting of numbers moving in clockwise direction around a rectangular shape (length and breadth decreasing each time)

How to print a 2 dimensional array in spiral order using JAVA? Please help, I don’t have any idea.

sample array:

1  2  3  4

5  6  7  8

9  10 11 12

13 14 15 16

output:

1 2 3 4 8 12 16 15 14 13 9 5 6 7 11 10

here is what i tried:

public static void main(String[] args) {

    int[][] values = {{1,2,3,4}, {5,6,7,8}, {9,10,11,12}, {13,14,15,16}};

    for (int i = (values.length * values[0].length)-1, j = 0; i > 0; i--, j++) {
          for (int k = j; k < i; k++) {
              System.out.print(values[j][k]);
          }

          for (int k = j; k < i; k++) {
              System.out.print(values[k][i]);
          }

          for (int k = i; k > j; k--) {
              System.out.print(values[i][k]);
          }

          for (int k = i; k > j; k--) {
              System.out.print(values[k][j]);
          }
    }

}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T08:24:18+00:00Added an answer on May 26, 2026 at 8:24 am

    here is some Java solution:

    public static final int X_SIZE = 4;
    public static final int Y_SIZE = 4;
    
    public static void main(String[] args) {
        int[][] array = new int[X_SIZE][Y_SIZE];
    
        for(int i = 0; i < X_SIZE; i++){
            for (int j = 0; j < Y_SIZE; j++){
                array[i][j] = i * X_SIZE + (j + 1);
                System.out.print(array[i][j] + " ");
            } 
            System.out.println();
        }
    
        System.out.println("************");
        System.out.println("Spiral");       
    
        spiralPrint(X_SIZE, Y_SIZE, array);
    }
    
    public static void spiralPrint(int xSize, int ySize, int matrix[][]){
        int i,  k = 0, l = 0;
        xSize--;  ySize--;      
    
        while(k <= xSize && l <= ySize){
            for(i = l; i <= ySize; ++i) {
                System.out.print(matrix[k][i]+ " ");
            }           
            k++;
    
            for(i = k; i <= xSize; ++i) {
                System.out.print(matrix[i][ySize] + " ");
            }
            ySize--;
    
            for(i = ySize; i >= l; --i) {
                    System.out.print(matrix[xSize][i] + " ");
            }
            xSize--;
    
    
            for(i = xSize; i >= k; --i) {
                System.out.print(matrix[i][l] + " ");
            }
            l++;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: byte[] array pattern search I am trying to write a simple compression
Possible Duplicate: Java \ Pattern - how to write a pattern that verifies the
Possible Duplicate: Design Pattern for Undo Engine In general, how do you deal with
Possible Duplicate: Join lines based on pattern I have the following file: test one
Possible Duplicate: Regex - nested patterns - within outer pattern but exclude inner pattern
Possible Duplicate: Learning to write a compiler I looked around trying to find out
Possible Duplicate: MVC pattern in Android? I want to follow MVC architecture in Android
Possible Duplicate: Performance difference between functions and pattern matching in Mathematica I often find
Possible Duplicate: C++ Singleton design pattern. How can I create only one instance of
Possible Duplicate: Is this key-oriented access-protection pattern a known idiom? I have class A

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.