Hi,

This is a very simple question. Is it possible to print a one dimensional array into the shape of a grid?

For example if I have the follow array:

chars arrayofChars[] = {'A', 'B', 'C', 'D', 'E'...};

how can it be printed out as

A B C D
E F G H
I J K L
...


I can print it out as a 2d array but prefer to use a 1D array if possible.

Any suggestions will be great.

Thanks,

Kimjack

Recommended Answers

All 11 Replies

You could use the escape sequence "\n" after you print out the 4th element in the array? Or use just System.out.println(); after the 4th element has been printed.

The second option is better as "\n" is platform specific and is not always guaranteed to get the effect you wanted.

... "\n" is platform specific and is not always guaranteed to get the effect you wanted.

Just in case this matters: you can use

System.getProperty("line.separator");

to get the line separator string appropriate to the current platform.

Yes. In fact thats very common, using 1d array as a mimic of a 2d.

final int ROW = 5;
final int COL  = 5

char[] Letters = new char[ ROW * COL];

for(int i = 0; i < ROW * COL; i++)
    Letters[i] = (char)( 'A' + i );

for(int i = 0; i < ROW; i++)
{
   for(int j = 0; j < COL; j++)
   {
       System.out.print( Letters[j + i * ROW] + " " );
   }
}

It hasn't been tested, but you should get the idea, if it doesn't work.

Yes. In fact thats very common, using 1d array as a mimic of a 2d.

final int ROW = 5;
final int COL  = 5

char[] Letters = new char[ ROW * COL];

for(int i = 0; i < ROW * COL; i++)
    Letters[i] = (char)( 'A' + i );

for(int i = 0; i < ROW; i++)
{
   for(int j = 0; j < COL; j++)
   {
       System.out.print( Letters[j + i * ROW] + " " );
   }
}

It hasn't been tested, but you should get the idea, if it doesn't work.

In this there is restriction that the array dimension can't be prime (Not a big deal...HUH??) :yawn:

for prime we can use following looping...

int j=0;
for(int i=0;i<arr.length;i++)
{
      while(j%4!=0)
      {
             System.out.print(arr[j++]+" ");
       }
       System.out.println(arr[j++]);
}

I hope this works well and maybe better (LOL :D)
your signature is really good "firstPerson" :)

This was was not working for me.

Here is what I have so far. In stead of printing it out in one row how can i get it to print in both rows and colomns. For example:

A B C D
E F G H
I J K L
...

Thanks

public void randomChars()
	{
		 ArrayList<Integer> list = new ArrayList<Integer>();
         int i = new Random().nextInt(letters.length);
         for(; list.size() != letters.length; i = new Random().nextInt(letters.length)) {
            if(!list.contains(i)) {
               System.out.print(letters[i]+" ");
               list.add(i);
            }
         }

In this there is restriction that the array dimension can't be prime

Why is that again?

This was was not working for me.

It works for me :

import java.*;


class Main
{
    static void Print(Object ob){
        System.out.print(ob);
    }
    static void Print() {
        System.out.println();
    }
    public static void main(String[] Arg)
    {
        final int ROW = 5;
        final int COL = 5;
        
        char[] fake2D = new char[ROW*COL];

        for(int i = 0; i < ROW*COL; i++)
            fake2D[i] = (char)('A' + i);

        for(int i = 0; i < ROW; i++)
        {
            for(int j = 0; j < COL; j++)
            {
                if(i > 0 && i % ROW == 0)
                    Print();

                Print(fake2D[j + i * ROW] + " ");
            }

            Print();
        }

    }

}

This is great, but how would I go about preventing duplicates when the array is randomized?

This is great, but how would I go about preventing duplicates when the array is randomized?

You could sort it and then check if the next element is the same and
work from there.

Why is that again?

You see as far as I understand.... the requirement was to put the display in matrix form and you've used ROW and COLUMN as the elements for deciding the matrix display... for a prime sized array this is not gonna work... or will it??:?:

This is great, but how would I go about preventing duplicates when the array is randomized?

What is the basic question that you are asking can you please explain if it is about printing 1D array in 2D format then where does the question comes about randomizing it??:?::-/

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.