1

I'm trying to create a method that takes a one dimensional array and prints it out as a two dimensional array, but as square as possible so it looks nice. I've tried creating for loops to do this, but how would you figure out how many rows and columns there are? Could someone give me the logic in how to make one, so I can use that to create my own? An explanation would be lovely.

  • What do you mean by nice? – HelloWorld123456789 Feb 24 '14 at 04:52
  • 2
    Your question is not clear at all. Add some codes to show what you tried, where are you exactly stuck at. This will help you get good answers. – Peshal Feb 24 '14 at 04:52
  • An explanation would indeed be lovely! What do you mean by printing a 1D array as a 2D array? You need to define your requirements better, and my guess is that once you do that, you'll have figured out how to implement it. As your question stands now, there's no way to help you, because there's no way to tell what you actually want. – Henry Keiter Feb 24 '14 at 04:52
  • Do you like have a language you love doing it in or just a pseudocode? – Zuko Feb 24 '14 at 04:52
  • 1
    take a look at [this](http://stackoverflow.com/questions/5134555/how-to-convert-a-1d-array-to-2d-array) – Zuko Feb 24 '14 at 05:07
  • `but how would you figure out how many rows and columns there are?` Rather than asking how you would actually turn a 1D array into a 2D array, you should start by asking yourself what it means to even turn a 1D array into a 2D array. If you don't know what the output should look like you're not going to be able to produce a solution. Come up with some base cases and then figure out what is the common point between them. – MxLDevs Feb 24 '14 at 05:14

4 Answers4

1

This will make a new array that is square and big enough to house all the elements of the old array. It takes the square root of the original array's length and rounds up.

int size = (int)Math.ceil(Math.sqrt(oldArray.length));
int[][]newArray = new int[size][size];
IamJohnny5
  • 33
  • 4
0

Example:

array =    |1 2 3|
           |4 5 6|
           |7 8 9| (3*3)

Make a 1D array

int array1 = new int[3*3];

Or you can get length of the array

For row length: int row = array.length;

For column length: int column = array[0].length;

and then int array1 = new int[row*column];

Now iterate over array and copy all elements of array into array1.

         _________________
array1 = |1|2|3|4|5|6|7|8|9|

Now don't ask me for code.

AJ.
  • 4,526
  • 5
  • 29
  • 41
0

This method will print the elements in your one-dimensional array as close as possible to a square pattern on the console output. It places tabs in between elements to line them up on each row.

private void printMyArray(String[]  onDimensionalArray) {
    int cols = (int) Math.floor(Math.sqrt(onDimensionalArray.length));
    int currentCol = 0;
    for(String element : onDimensionalArray) {
        System.out.print(element + "\t");
        if(currentCol >= cols) {
            System.out.println("");
            currentCol = 0;
        }
        else {
            currentCol++;
        }
    }
}
Fergal
  • 5,213
  • 6
  • 35
  • 44
-1

check this out

int array2d[][] = new int[10][3];


for(int i=0; i<10;i++)
for(int j=0;j<3;j++)
   array2d[i][j] = array1d[(j*10) + i];
Zuko
  • 2,764
  • 30
  • 30