| | |
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.
Computers are man's attempt at designing a cat: It does whatever it wants, whenever it wants, and rarely ever at the right time.
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: 979
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.
1) What word becomes shorter if you add a letter to it?
[ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666, jonsca]
2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
[*solved by : murtan, xavier666]
3) What is the 123456789th prime numer?•
•
•
•
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
1) What word becomes shorter if you add a letter to it?
[ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666, jonsca]
2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
[*solved by : murtan, xavier666]
3) What is the 123456789th prime numer? 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(); } } }
1) What word becomes shorter if you add a letter to it?
[ Solved by : niek_e, Paul Thompson, SgtMe, murtan, xavier666, jonsca]
2) What does this sequence equal to : (.5u - .5a)(.5u-.5b)(.5u-.5c) ...
[*solved by : murtan, xavier666]
3) What is the 123456789th prime numer?![]() |
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 |
add android api applet application applications array arrays automation bank binary bluetooth chat class classes clear client code component converter database db development dice digit draw eclipse equation error event exception formatingtextintooltipjava fractal functiontesting game givemetehcodez graphics gui health html hyper ide idea image infinite input int integer j2me java javame javaprojects jni jpanel julia linux list loop main map method methods mobile myregfun netbeans newbie nonstatic openjavafx oracle pearl print problem program programming project recursion repositories scanner screen scrollbar server set size sms sort sorting spamblocker sql sqlserver state storm string superclass swing text-file thread threads time tree windows






