| | |
Simple Array Question
Please support our Java advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved |
•
•
Join Date: Apr 2006
Posts: 114
Reputation:
Solved Threads: 0
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
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
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!
If you got your answer, please mark the thread as Solved. It saves time when people are looking to contribute threads or for answers!
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
----------------------------------------------
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
•
•
Join Date: Apr 2008
Posts: 972
Reputation:
Solved Threads: 145
•
•
•
•
... "\n" is platform specific and is not always guaranteed to get the effect you wanted.
Java Syntax (Toggle Plain Text)
System.getProperty("line.separator");
Yes. In fact thats very common, using 1d array as a mimic of a 2d.
It hasn't been tested, but you should get the idea, if it doesn't work.
Java Syntax (Toggle Plain Text)
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.
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
•
•
•
•
Yes. In fact thats very common, using 1d array as a mimic of a 2d.
Java Syntax (Toggle Plain Text)
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??)

for prime we can use following looping...
Java Syntax (Toggle Plain Text)
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
) 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.
"I M POSSIBLE":) :) :)
#Your I Can is more important than your I.Q.
•
•
Join Date: Apr 2006
Posts: 114
Reputation:
Solved Threads: 0
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
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)
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); } }
Last edited by KimJack; Oct 3rd, 2009 at 6:48 pm.
•
•
•
•
In this there is restriction that the array dimension can't be prime
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
It works for me :
Java Syntax (Toggle Plain Text)
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(); } } }
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
![]() |
Similar Threads
- Simple Array() question (PHP)
- Little Simple array question :) (C)
- Simple array question (C++)
- Simple 2d array help (Visual Basic 4 / 5 / 6)
Other Threads in the Java Forum
- Previous Thread: password gen assignment
- Next Thread: Java - Outputting toString with memory value rather than the actual string value
| Thread Tools | Search this Thread |
911 actionlistener addressbook android api append applet application array arrays automation binary block bluetooth character chat class client code component consumer csv database desktop eclipse error fractal ftp game givemetehcodez graphics gui html ide image input integer j2me japplet java javaarraylist javac javaee javaprojects jmf jni jpanel julia linked linux list loop mac map method methods mobile netbeans newbie number objects online oriented panel print printf problem program programming project projects properties recursion replaydirector reporting researchinmotion rotatetext rsa scanner screen se server set size sms sort sql string swing template test threads time title tree tutorial-sample ubuntu update windows working






