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

  • Home
  • SEARCH
  • 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 8067433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:17:29+00:00 2026-06-05T12:17:29+00:00

I want to create a multi-dimensional array from a single dimensional array in java.

  • 0

I want to create a multi-dimensional array from a single dimensional array in java.

my single dimenstional array is like

  int[] grid = {5, 3, 1, 2, 0, 4, 1, 1, 3 };

I want to create a matrix of 3×3 diminsions at runtime as there are 9 elements in this array. Can anyone suggest some good idea for this.
Can anyone suggest me what should I do in below code so that it can give me arraylist of 3 arrays or if someone can suggust me some better idea to perform this correctly. I know there is error in my below code.

         ArrayList<int[]> matrix = new ArrayList<>();
    int[] tempArray = new int[n];
    int j = 0;
    for(int i=0 ; i<=grid.length; i++){         

        if((i+1) / 3 == 0){         
            matrix.add(tempArray);
            j=0;
        }else{          
            tempArray[j] = grid[i];
            j++;
        }
    }   

Thanks

  • 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-06-05T12:17:32+00:00Added an answer on June 5, 2026 at 12:17 pm

    Java does multi-dimensional arrays natively; you don’t need to create your own using arrays in an ArrayList.

    Something like this should give you what you want:

    int[] grid = {5, 3, 1, 2, 0, 4, 1, 1, 3 };
    int [][] matrix = new int[3][3];
    
    for (int x = 0; x < 3; x++) {
      for (int y = 0; y < 3; y++) {
        matrix[x][y] = grid[x + y * 3];
      }   
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want PHP to randomly create a multi-dimensional array by picking a vast amount
I want to create an multidimensional array in Java, like the PHP one shown
I want to create a multi-module (maven) spring MVC application, with modules like: web
The array is multi-dimensional and has a variable number of sub-keys, like $arr[$a][$b][$c] =
I have a multi-dimensional array that looks like this: The base array is indexed
I want to create a mutli dimensional array without a fixed size. I need
I have a non-fixed dimensional matrix M, from which I want to access a
Basically I want to create a multidimensional array (array of arrays) with data from
I want to create a strongly typed multidimensional array or collection containing the following
I want to create a Python dictionary which holds values in a multidimensional array

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.