954,536 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Simple Array Question

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

KimJack
Junior Poster
114 posts since Apr 2006
Reputation Points: 8
Solved Threads: 0
 

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.

majestic0110
Nearly a Posting Virtuoso
1,328 posts since Oct 2007
Reputation Points: 256
Solved Threads: 72
 

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

masijade
Industrious Poster
Moderator
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
 
... "\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.

JamesCherrill
Posting Genius
Moderator
6,373 posts since Apr 2008
Reputation Points: 2,130
Solved Threads: 1,073
 

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.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

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" :)

anupam_smart
Posting Pro
599 posts since Feb 2006
Reputation Points: 15
Solved Threads: 4
 

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);
            }
         }
KimJack
Junior Poster
114 posts since Apr 2006
Reputation Points: 8
Solved Threads: 0
 
In this there is restriction that the array dimension can't be prime

Why is that again?

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 
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();
        }

    }

}
firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 

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

KimJack
Junior Poster
114 posts since Apr 2006
Reputation Points: 8
Solved Threads: 0
 
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.

firstPerson
Senior Poster
3,923 posts since Dec 2008
Reputation Points: 841
Solved Threads: 608
 
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??:?::-/

anupam_smart
Posting Pro
599 posts since Feb 2006
Reputation Points: 15
Solved Threads: 4
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You