1

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
2
  • 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? Commented Nov 10, 2018 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. Commented Nov 10, 2018 at 4:39

2 Answers 2

5

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();
}
Sign up to request clarification or add additional context in comments.

4 Comments

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 Also view other answers before accepting an answer. There may be better ways of achieving this. Just saying.
@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 :)
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
1

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 Comment

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.