Simple Array Question

Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Apr 2006
Posts: 114
Reputation: KimJack is an unknown quantity at this point 
Solved Threads: 0
KimJack KimJack is offline Offline
Junior Poster

Simple Array Question

 
0
  #1
Oct 3rd, 2009
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
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,293
Reputation: majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about majestic0110 has a spectacular aura about 
Solved Threads: 67
majestic0110's Avatar
majestic0110 majestic0110 is offline Offline
Nearly a Posting Virtuoso

Re: Simple Array Question

 
0
  #2
Oct 3rd, 2009
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.
If you have a quality, be proud of it and let it define you. Add it to the world!
If you got your answer, please mark the thread as Solved. It saves time when people are looking to contribute threads or for answers!
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 2,391
Reputation: masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of masijade has much to be proud of 
Solved Threads: 254
Moderator
masijade's Avatar
masijade masijade is offline Offline
Nearly a Posting Maven

Re: Simple Array Question

 
0
  #3
Oct 3rd, 2009
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.
Java Programmer and Sun Systems Administrator

----------------------------------------------

Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it.
--Brian Kernighan
Reply With Quote Quick reply to this message  
Join Date: Apr 2008
Posts: 972
Reputation: JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice JamesCherrill is just really nice 
Solved Threads: 145
JamesCherrill JamesCherrill is offline Offline
Posting Shark

Re: Simple Array Question

 
0
  #4
Oct 3rd, 2009
Originally Posted by masijade View Post
... "\n" is platform specific and is not always guaranteed to get the effect you wanted.
Just in case this matters: you can use
  1. System.getProperty("line.separator");
to get the line separator string appropriate to the current platform.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,218
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 149
firstPerson's Avatar
firstPerson firstPerson is online now Online
Nearly a Posting Virtuoso

Re: Simple Array Question

 
0
  #5
Oct 3rd, 2009
Yes. In fact thats very common, using 1d array as a mimic of a 2d.

  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.
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Ask4Answer
Reply With Quote Quick reply to this message  
Join Date: Feb 2006
Posts: 598
Reputation: anupam_smart is an unknown quantity at this point 
Solved Threads: 4
anupam_smart's Avatar
anupam_smart anupam_smart is offline Offline
Posting Pro

Re: Simple Array Question

 
-1
  #6
Oct 3rd, 2009
Originally Posted by firstPerson View Post
Yes. In fact thats very common, using 1d array as a mimic of a 2d.

  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...
  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"
# Never say impossible 'cause impossible itself says:

"I M POSSIBLE":) :) :)

#Your I Can is more important than your I.Q.
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 114
Reputation: KimJack is an unknown quantity at this point 
Solved Threads: 0
KimJack KimJack is offline Offline
Junior Poster

Re: Simple Array Question

 
0
  #7
Oct 3rd, 2009
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

  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.
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,218
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 149
firstPerson's Avatar
firstPerson firstPerson is online now Online
Nearly a Posting Virtuoso

Re: Simple Array Question

 
-1
  #8
Oct 3rd, 2009
In this there is restriction that the array dimension can't be prime
Why is that again?
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Ask4Answer
Reply With Quote Quick reply to this message  
Join Date: Dec 2008
Posts: 1,218
Reputation: firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice firstPerson is just really nice 
Solved Threads: 149
firstPerson's Avatar
firstPerson firstPerson is online now Online
Nearly a Posting Virtuoso

Re: Simple Array Question

 
-1
  #9
Oct 3rd, 2009
Originally Posted by KimJack View Post
This was was not working for me.
It works for me :
  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. }
I give up! 
1) What word becomes shorter if you add a letter to it? [ Solved by : niek_e ]
2) What does this sequence  equal to :  (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
3) What is the 123456789 prime numer?
Ask4Answer
Reply With Quote Quick reply to this message  
Join Date: Apr 2006
Posts: 114
Reputation: KimJack is an unknown quantity at this point 
Solved Threads: 0
KimJack KimJack is offline Offline
Junior Poster

Re: Simple Array Question

 
0
  #10
Oct 3rd, 2009
This is great, but how would I go about preventing duplicates when the array is randomized?
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC