944,204 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Marked Solved
  • Views: 921
  • Java RSS
You are currently viewing page 1 of this multi-page discussion thread
Oct 3rd, 2009
0

Simple Array Question

Expand Post »
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
Similar Threads
Reputation Points: 8
Solved Threads: 0
Junior Poster
KimJack is offline Offline
114 posts
since Apr 2006
Oct 3rd, 2009
0

Re: Simple Array Question

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.
Last edited by majestic0110; Oct 3rd, 2009 at 10:59 am.
Reputation Points: 256
Solved Threads: 72
Nearly a Posting Virtuoso
majestic0110 is offline Offline
1,306 posts
since Oct 2007
Oct 3rd, 2009
0

Re: Simple Array Question

The second option is better as "\n" is platform specific and is not always guaranteed to get the effect you wanted.
Last edited by masijade; Oct 3rd, 2009 at 11:05 am.
Moderator
Reputation Points: 1471
Solved Threads: 490
Industrious Poster
masijade is offline Offline
4,043 posts
since Feb 2006
Oct 3rd, 2009
0

Re: Simple Array Question

Click to Expand / Collapse  Quote originally posted by masijade ...
... "\n" is platform specific and is not always guaranteed to get the effect you wanted.
Just in case this matters: you can use
Java Syntax (Toggle Plain Text)
  1. System.getProperty("line.separator");
to get the line separator string appropriate to the current platform.
Featured Poster
Reputation Points: 1939
Solved Threads: 955
Posting Expert
JamesCherrill is online now Online
5,815 posts
since Apr 2008
Oct 3rd, 2009
0

Re: Simple Array Question

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

Java Syntax (Toggle Plain Text)
  1. final int ROW = 5;
  2. final int COL = 5
  3.  
  4. char[] Letters = new char[ ROW * COL];
  5.  
  6. for(int i = 0; i < ROW * COL; i++)
  7. Letters[i] = (char)( 'A' + i );
  8.  
  9. for(int i = 0; i < ROW; i++)
  10. {
  11. for(int j = 0; j < COL; j++)
  12. {
  13. System.out.print( Letters[j + i * ROW] + " " );
  14. }
  15. }

It hasn't been tested, but you should get the idea, if it doesn't work.
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,865 posts
since Dec 2008
Oct 3rd, 2009
-1

Re: Simple Array Question

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

Java Syntax (Toggle Plain Text)
  1. final int ROW = 5;
  2. final int COL = 5
  3.  
  4. char[] Letters = new char[ ROW * COL];
  5.  
  6. for(int i = 0; i < ROW * COL; i++)
  7. Letters[i] = (char)( 'A' + i );
  8.  
  9. for(int i = 0; i < ROW; i++)
  10. {
  11. for(int j = 0; j < COL; j++)
  12. {
  13. System.out.print( Letters[j + i * ROW] + " " );
  14. }
  15. }

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??)

for prime we can use following looping...
Java Syntax (Toggle Plain Text)
  1. int j=0;
  2. for(int i=0;i<arr.length;i++)
  3. {
  4. while(j%4!=0)
  5. {
  6. System.out.print(arr[j++]+" ");
  7. }
  8. System.out.println(arr[j++]);
  9. }

I hope this works well and maybe better (LOL )
your signature is really good "firstPerson"
Reputation Points: 15
Solved Threads: 4
Posting Pro
anupam_smart is offline Offline
598 posts
since Feb 2006
Oct 3rd, 2009
0

Re: Simple Array Question

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

Java Syntax (Toggle Plain Text)
  1. public void randomChars()
  2. {
  3. ArrayList<Integer> list = new ArrayList<Integer>();
  4. int i = new Random().nextInt(letters.length);
  5. for(; list.size() != letters.length; i = new Random().nextInt(letters.length)) {
  6. if(!list.contains(i)) {
  7. System.out.print(letters[i]+" ");
  8. list.add(i);
  9. }
  10. }
Last edited by KimJack; Oct 3rd, 2009 at 6:48 pm.
Reputation Points: 8
Solved Threads: 0
Junior Poster
KimJack is offline Offline
114 posts
since Apr 2006
Oct 3rd, 2009
-1

Re: Simple Array Question

Quote ...
In this there is restriction that the array dimension can't be prime
Why is that again?
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,865 posts
since Dec 2008
Oct 3rd, 2009
-1

Re: Simple Array Question

Click to Expand / Collapse  Quote originally posted by KimJack ...
This was was not working for me.
It works for me :
Java Syntax (Toggle Plain Text)
  1. import java.*;
  2.  
  3.  
  4. class Main
  5. {
  6. static void Print(Object ob){
  7. System.out.print(ob);
  8. }
  9. static void Print() {
  10. System.out.println();
  11. }
  12. public static void main(String[] Arg)
  13. {
  14. final int ROW = 5;
  15. final int COL = 5;
  16.  
  17. char[] fake2D = new char[ROW*COL];
  18.  
  19. for(int i = 0; i < ROW*COL; i++)
  20. fake2D[i] = (char)('A' + i);
  21.  
  22. for(int i = 0; i < ROW; i++)
  23. {
  24. for(int j = 0; j < COL; j++)
  25. {
  26. if(i > 0 && i % ROW == 0)
  27. Print();
  28.  
  29. Print(fake2D[j + i * ROW] + " ");
  30. }
  31.  
  32. Print();
  33. }
  34.  
  35. }
  36.  
  37. }
Reputation Points: 840
Solved Threads: 594
Senior Poster
firstPerson is offline Offline
3,865 posts
since Dec 2008
Oct 3rd, 2009
0

Re: Simple Array Question

This is great, but how would I go about preventing duplicates when the array is randomized?
Reputation Points: 8
Solved Threads: 0
Junior Poster
KimJack is offline Offline
114 posts
since Apr 2006

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: password gen assignment
Next Thread in Java Forum Timeline: Java - Outputting toString with memory value rather than the actual string value





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC