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









share|improve this question























  • 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














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









share|improve this question























  • 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












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









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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
















  • 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












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();






share|improve this answer






















  • 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

















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;






share|improve this answer




















  • +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










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















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

























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();






share|improve this answer






















  • 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














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();






share|improve this answer






















  • 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












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();






share|improve this answer














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();







share|improve this answer














share|improve this answer



share|improve this answer








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
















  • 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












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;






share|improve this answer




















  • +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














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;






share|improve this answer




















  • +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












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;






share|improve this answer












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;







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 10 at 4:42









saeedata

38616




38616











  • +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















+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

















draft saved

draft discarded
















































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.




draft saved


draft discarded














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





















































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







Popular posts from this blog

Kleinkühnau

Makov (Slowakei)

Deutsches Schauspielhaus