Converting 2D integer array to String
up vote
1
down vote
favorite
I made a 2D array containing integer values and want to convert it into a string, but I'm really confused on what I'm able to put into //something to get the output I want.
public static void main(String args) {
final int test = 1, 6, 11, 16, 21,
2, 7, 12, 17, 22,
3, 8, 13, 18, 23,
4, 9, 14, 19, 24,
5, 10, 15, 20, 25 ;
System.out.println(TwoDOneD.DownNUp(test));
public static String DownNUp(int test)
String res = "";
for (int r = 0; r < test[0].length; r++)
for (int c = 0; c < test.length; c++)
if (c % 2 == 1)
//something
else
res += test[c][r] + " ";
return res;
The output I'm trying to get is why I have (c % 2 == 1); in every odd column, it's supposed to go down, and in every even column, it goes right back up.
1 2 3 4 5 10 9 8 7 6 11 12 13 14 15 20 19 18 17 16 21 22 23 24 25
java
add a comment |
up vote
1
down vote
favorite
I made a 2D array containing integer values and want to convert it into a string, but I'm really confused on what I'm able to put into //something to get the output I want.
public static void main(String args) {
final int test = 1, 6, 11, 16, 21,
2, 7, 12, 17, 22,
3, 8, 13, 18, 23,
4, 9, 14, 19, 24,
5, 10, 15, 20, 25 ;
System.out.println(TwoDOneD.DownNUp(test));
public static String DownNUp(int test)
String res = "";
for (int r = 0; r < test[0].length; r++)
for (int c = 0; c < test.length; c++)
if (c % 2 == 1)
//something
else
res += test[c][r] + " ";
return res;
The output I'm trying to get is why I have (c % 2 == 1); in every odd column, it's supposed to go down, and in every even column, it goes right back up.
1 2 3 4 5 10 9 8 7 6 11 12 13 14 15 20 19 18 17 16 21 22 23 24 25
java
It's in every odd column because it seems that c%2 == 0 is doing nothing in the if block. If you want to convert entire array to string, why have the check?
– Sid
Nov 10 at 4:34
@Sid That's because there's nothing but a comment in the c%2 == 1, which is where I would put the actual code. And I want the entire array to be converted into a string in that format.
– Excel
Nov 10 at 4:39
add a comment |
up vote
1
down vote
favorite
up vote
1
down vote
favorite
I made a 2D array containing integer values and want to convert it into a string, but I'm really confused on what I'm able to put into //something to get the output I want.
public static void main(String args) {
final int test = 1, 6, 11, 16, 21,
2, 7, 12, 17, 22,
3, 8, 13, 18, 23,
4, 9, 14, 19, 24,
5, 10, 15, 20, 25 ;
System.out.println(TwoDOneD.DownNUp(test));
public static String DownNUp(int test)
String res = "";
for (int r = 0; r < test[0].length; r++)
for (int c = 0; c < test.length; c++)
if (c % 2 == 1)
//something
else
res += test[c][r] + " ";
return res;
The output I'm trying to get is why I have (c % 2 == 1); in every odd column, it's supposed to go down, and in every even column, it goes right back up.
1 2 3 4 5 10 9 8 7 6 11 12 13 14 15 20 19 18 17 16 21 22 23 24 25
java
I made a 2D array containing integer values and want to convert it into a string, but I'm really confused on what I'm able to put into //something to get the output I want.
public static void main(String args) {
final int test = 1, 6, 11, 16, 21,
2, 7, 12, 17, 22,
3, 8, 13, 18, 23,
4, 9, 14, 19, 24,
5, 10, 15, 20, 25 ;
System.out.println(TwoDOneD.DownNUp(test));
public static String DownNUp(int test)
String res = "";
for (int r = 0; r < test[0].length; r++)
for (int c = 0; c < test.length; c++)
if (c % 2 == 1)
//something
else
res += test[c][r] + " ";
return res;
The output I'm trying to get is why I have (c % 2 == 1); in every odd column, it's supposed to go down, and in every even column, it goes right back up.
1 2 3 4 5 10 9 8 7 6 11 12 13 14 15 20 19 18 17 16 21 22 23 24 25
java
java
edited Nov 10 at 16:13
Mikhail Kholodkov
3,66252343
3,66252343
asked Nov 10 at 4:28
Excel
478
478
It's in every odd column because it seems that c%2 == 0 is doing nothing in the if block. If you want to convert entire array to string, why have the check?
– Sid
Nov 10 at 4:34
@Sid That's because there's nothing but a comment in the c%2 == 1, which is where I would put the actual code. And I want the entire array to be converted into a string in that format.
– Excel
Nov 10 at 4:39
add a comment |
It's in every odd column because it seems that c%2 == 0 is doing nothing in the if block. If you want to convert entire array to string, why have the check?
– Sid
Nov 10 at 4:34
@Sid That's because there's nothing but a comment in the c%2 == 1, which is where I would put the actual code. And I want the entire array to be converted into a string in that format.
– Excel
Nov 10 at 4:39
It's in every odd column because it seems that c%2 == 0 is doing nothing in the if block. If you want to convert entire array to string, why have the check?
– Sid
Nov 10 at 4:34
It's in every odd column because it seems that c%2 == 0 is doing nothing in the if block. If you want to convert entire array to string, why have the check?
– Sid
Nov 10 at 4:34
@Sid That's because there's nothing but a comment in the c%2 == 1, which is where I would put the actual code. And I want the entire array to be converted into a string in that format.
– Excel
Nov 10 at 4:39
@Sid That's because there's nothing but a comment in the c%2 == 1, which is where I would put the actual code. And I want the entire array to be converted into a string in that format.
– Excel
Nov 10 at 4:39
add a comment |
2 Answers
2
active
oldest
votes
up vote
4
down vote
accepted
You need to change iteration direction, based on c % 2 == 0
public static void main(String args)
final int test = 1, 6, 11, 16, 21,
2, 7, 12, 17, 22,
3, 8, 13, 18, 23,
4, 9, 14, 19, 24,
5, 10, 15, 20, 25 ;
System.out.println(downUp(test));
public static String downUp(int test)
String res = "";
for (int c = 0; c < test[0].length; c++)
if (c % 2 == 0)
for (int r = 0; r < test.length; r++)
res += test[r][c] + " ";
else
for (int r = test.length -1 ; r >=0 ; r--)
res += test[r][c] + " ";
return res;
A side note: it would be better implemented using a StringBuilder like so:
public static String downUp(int test)
StringBuilder res = new StringBuilder();
for (int c = 0; c < test[0].length; c++)
if (c % 2 == 0)
for (int r = 0; r < test.length; r++)
res.append(test[r][c]).append(" ");
else
for (int r = test.length -1 ; r >=0 ; r--)
res.append(test[r][c]).append(" ");
return res.toString();
Worked perfectly. I'll be sure to keep in mind that when I want to change the direction that I'm running to change the iteration.
– Excel
Nov 10 at 4:46
1
@Excel Also view other answers before accepting an answer. There may be better ways of achieving this. Just saying.
– Sid
Nov 10 at 4:46
1
@Sid I'll be sure to look for more efficient ways that I can understand at the level I'm at in my coding, thanks :)
– Excel
Nov 10 at 4:51
1
I agree with @Sid. What I posted is aimed at making the change of iteration direction clear and simple. There are other, more concise ways to do so like saeedata proposed
– c0der
Nov 10 at 4:58
add a comment |
up vote
1
down vote
i think this is that you want:
public static String DownNUp(int test)
String res = "";
for (int r = 0; r < test[0].length; r++)
for (int c = 0; c < test.length; c++)
if (r % 2 != 0)
res += test[test.length - 1 - c][r] + " ";
else
res += test[c][r] + " ";
return res;
+1 . I think naming could be better if you changedr(which represents column) tocandctor. Also for method name usedownUp.
– c0der
Nov 10 at 4:52
add a comment |
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
4
down vote
accepted
You need to change iteration direction, based on c % 2 == 0
public static void main(String args)
final int test = 1, 6, 11, 16, 21,
2, 7, 12, 17, 22,
3, 8, 13, 18, 23,
4, 9, 14, 19, 24,
5, 10, 15, 20, 25 ;
System.out.println(downUp(test));
public static String downUp(int test)
String res = "";
for (int c = 0; c < test[0].length; c++)
if (c % 2 == 0)
for (int r = 0; r < test.length; r++)
res += test[r][c] + " ";
else
for (int r = test.length -1 ; r >=0 ; r--)
res += test[r][c] + " ";
return res;
A side note: it would be better implemented using a StringBuilder like so:
public static String downUp(int test)
StringBuilder res = new StringBuilder();
for (int c = 0; c < test[0].length; c++)
if (c % 2 == 0)
for (int r = 0; r < test.length; r++)
res.append(test[r][c]).append(" ");
else
for (int r = test.length -1 ; r >=0 ; r--)
res.append(test[r][c]).append(" ");
return res.toString();
Worked perfectly. I'll be sure to keep in mind that when I want to change the direction that I'm running to change the iteration.
– Excel
Nov 10 at 4:46
1
@Excel Also view other answers before accepting an answer. There may be better ways of achieving this. Just saying.
– Sid
Nov 10 at 4:46
1
@Sid I'll be sure to look for more efficient ways that I can understand at the level I'm at in my coding, thanks :)
– Excel
Nov 10 at 4:51
1
I agree with @Sid. What I posted is aimed at making the change of iteration direction clear and simple. There are other, more concise ways to do so like saeedata proposed
– c0der
Nov 10 at 4:58
add a comment |
up vote
4
down vote
accepted
You need to change iteration direction, based on c % 2 == 0
public static void main(String args)
final int test = 1, 6, 11, 16, 21,
2, 7, 12, 17, 22,
3, 8, 13, 18, 23,
4, 9, 14, 19, 24,
5, 10, 15, 20, 25 ;
System.out.println(downUp(test));
public static String downUp(int test)
String res = "";
for (int c = 0; c < test[0].length; c++)
if (c % 2 == 0)
for (int r = 0; r < test.length; r++)
res += test[r][c] + " ";
else
for (int r = test.length -1 ; r >=0 ; r--)
res += test[r][c] + " ";
return res;
A side note: it would be better implemented using a StringBuilder like so:
public static String downUp(int test)
StringBuilder res = new StringBuilder();
for (int c = 0; c < test[0].length; c++)
if (c % 2 == 0)
for (int r = 0; r < test.length; r++)
res.append(test[r][c]).append(" ");
else
for (int r = test.length -1 ; r >=0 ; r--)
res.append(test[r][c]).append(" ");
return res.toString();
Worked perfectly. I'll be sure to keep in mind that when I want to change the direction that I'm running to change the iteration.
– Excel
Nov 10 at 4:46
1
@Excel Also view other answers before accepting an answer. There may be better ways of achieving this. Just saying.
– Sid
Nov 10 at 4:46
1
@Sid I'll be sure to look for more efficient ways that I can understand at the level I'm at in my coding, thanks :)
– Excel
Nov 10 at 4:51
1
I agree with @Sid. What I posted is aimed at making the change of iteration direction clear and simple. There are other, more concise ways to do so like saeedata proposed
– c0der
Nov 10 at 4:58
add a comment |
up vote
4
down vote
accepted
up vote
4
down vote
accepted
You need to change iteration direction, based on c % 2 == 0
public static void main(String args)
final int test = 1, 6, 11, 16, 21,
2, 7, 12, 17, 22,
3, 8, 13, 18, 23,
4, 9, 14, 19, 24,
5, 10, 15, 20, 25 ;
System.out.println(downUp(test));
public static String downUp(int test)
String res = "";
for (int c = 0; c < test[0].length; c++)
if (c % 2 == 0)
for (int r = 0; r < test.length; r++)
res += test[r][c] + " ";
else
for (int r = test.length -1 ; r >=0 ; r--)
res += test[r][c] + " ";
return res;
A side note: it would be better implemented using a StringBuilder like so:
public static String downUp(int test)
StringBuilder res = new StringBuilder();
for (int c = 0; c < test[0].length; c++)
if (c % 2 == 0)
for (int r = 0; r < test.length; r++)
res.append(test[r][c]).append(" ");
else
for (int r = test.length -1 ; r >=0 ; r--)
res.append(test[r][c]).append(" ");
return res.toString();
You need to change iteration direction, based on c % 2 == 0
public static void main(String args)
final int test = 1, 6, 11, 16, 21,
2, 7, 12, 17, 22,
3, 8, 13, 18, 23,
4, 9, 14, 19, 24,
5, 10, 15, 20, 25 ;
System.out.println(downUp(test));
public static String downUp(int test)
String res = "";
for (int c = 0; c < test[0].length; c++)
if (c % 2 == 0)
for (int r = 0; r < test.length; r++)
res += test[r][c] + " ";
else
for (int r = test.length -1 ; r >=0 ; r--)
res += test[r][c] + " ";
return res;
A side note: it would be better implemented using a StringBuilder like so:
public static String downUp(int test)
StringBuilder res = new StringBuilder();
for (int c = 0; c < test[0].length; c++)
if (c % 2 == 0)
for (int r = 0; r < test.length; r++)
res.append(test[r][c]).append(" ");
else
for (int r = test.length -1 ; r >=0 ; r--)
res.append(test[r][c]).append(" ");
return res.toString();
edited Nov 10 at 4:46
answered Nov 10 at 4:41
c0der
7,62841644
7,62841644
Worked perfectly. I'll be sure to keep in mind that when I want to change the direction that I'm running to change the iteration.
– Excel
Nov 10 at 4:46
1
@Excel Also view other answers before accepting an answer. There may be better ways of achieving this. Just saying.
– Sid
Nov 10 at 4:46
1
@Sid I'll be sure to look for more efficient ways that I can understand at the level I'm at in my coding, thanks :)
– Excel
Nov 10 at 4:51
1
I agree with @Sid. What I posted is aimed at making the change of iteration direction clear and simple. There are other, more concise ways to do so like saeedata proposed
– c0der
Nov 10 at 4:58
add a comment |
Worked perfectly. I'll be sure to keep in mind that when I want to change the direction that I'm running to change the iteration.
– Excel
Nov 10 at 4:46
1
@Excel Also view other answers before accepting an answer. There may be better ways of achieving this. Just saying.
– Sid
Nov 10 at 4:46
1
@Sid I'll be sure to look for more efficient ways that I can understand at the level I'm at in my coding, thanks :)
– Excel
Nov 10 at 4:51
1
I agree with @Sid. What I posted is aimed at making the change of iteration direction clear and simple. There are other, more concise ways to do so like saeedata proposed
– c0der
Nov 10 at 4:58
Worked perfectly. I'll be sure to keep in mind that when I want to change the direction that I'm running to change the iteration.
– Excel
Nov 10 at 4:46
Worked perfectly. I'll be sure to keep in mind that when I want to change the direction that I'm running to change the iteration.
– Excel
Nov 10 at 4:46
1
1
@Excel Also view other answers before accepting an answer. There may be better ways of achieving this. Just saying.
– Sid
Nov 10 at 4:46
@Excel Also view other answers before accepting an answer. There may be better ways of achieving this. Just saying.
– Sid
Nov 10 at 4:46
1
1
@Sid I'll be sure to look for more efficient ways that I can understand at the level I'm at in my coding, thanks :)
– Excel
Nov 10 at 4:51
@Sid I'll be sure to look for more efficient ways that I can understand at the level I'm at in my coding, thanks :)
– Excel
Nov 10 at 4:51
1
1
I agree with @Sid. What I posted is aimed at making the change of iteration direction clear and simple. There are other, more concise ways to do so like saeedata proposed
– c0der
Nov 10 at 4:58
I agree with @Sid. What I posted is aimed at making the change of iteration direction clear and simple. There are other, more concise ways to do so like saeedata proposed
– c0der
Nov 10 at 4:58
add a comment |
up vote
1
down vote
i think this is that you want:
public static String DownNUp(int test)
String res = "";
for (int r = 0; r < test[0].length; r++)
for (int c = 0; c < test.length; c++)
if (r % 2 != 0)
res += test[test.length - 1 - c][r] + " ";
else
res += test[c][r] + " ";
return res;
+1 . I think naming could be better if you changedr(which represents column) tocandctor. Also for method name usedownUp.
– c0der
Nov 10 at 4:52
add a comment |
up vote
1
down vote
i think this is that you want:
public static String DownNUp(int test)
String res = "";
for (int r = 0; r < test[0].length; r++)
for (int c = 0; c < test.length; c++)
if (r % 2 != 0)
res += test[test.length - 1 - c][r] + " ";
else
res += test[c][r] + " ";
return res;
+1 . I think naming could be better if you changedr(which represents column) tocandctor. Also for method name usedownUp.
– c0der
Nov 10 at 4:52
add a comment |
up vote
1
down vote
up vote
1
down vote
i think this is that you want:
public static String DownNUp(int test)
String res = "";
for (int r = 0; r < test[0].length; r++)
for (int c = 0; c < test.length; c++)
if (r % 2 != 0)
res += test[test.length - 1 - c][r] + " ";
else
res += test[c][r] + " ";
return res;
i think this is that you want:
public static String DownNUp(int test)
String res = "";
for (int r = 0; r < test[0].length; r++)
for (int c = 0; c < test.length; c++)
if (r % 2 != 0)
res += test[test.length - 1 - c][r] + " ";
else
res += test[c][r] + " ";
return res;
answered Nov 10 at 4:42
saeedata
38616
38616
+1 . I think naming could be better if you changedr(which represents column) tocandctor. Also for method name usedownUp.
– c0der
Nov 10 at 4:52
add a comment |
+1 . I think naming could be better if you changedr(which represents column) tocandctor. Also for method name usedownUp.
– c0der
Nov 10 at 4:52
+1 . I think naming could be better if you changed
r (which represents column) to c and c to r. Also for method name use downUp.– c0der
Nov 10 at 4:52
+1 . I think naming could be better if you changed
r (which represents column) to c and c to r. Also for method name use downUp.– c0der
Nov 10 at 4:52
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53235993%2fconverting-2d-integer-array-to-string%23new-answer', 'question_page');
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
It's in every odd column because it seems that c%2 == 0 is doing nothing in the if block. If you want to convert entire array to string, why have the check?
– Sid
Nov 10 at 4:34
@Sid That's because there's nothing but a comment in the c%2 == 1, which is where I would put the actual code. And I want the entire array to be converted into a string in that format.
– Excel
Nov 10 at 4:39