-2

I'm attempting to set up a 2 dimensional array of the alphabet in java. My first attempt (doesn't work):

char[][] arr=new char[4][7];
for(int r=0; r<arr.length; r++){
   for(int c=0; c<arr[r].length; c++){
      for(char i=65; i<91; i++){
         arr[r][c]=i;
         System.out.print(arr[r][c]);
      }
   }
   System.out.println();
}

My second attempt(does work, but seems convoluted):

    char[][] arr2=new char[4][7];
    char i=65;
    for(int r=0; r<arr2.length; r++){
        for(int c=0; c<arr2[r].length; c++){
            arr2[r][c]=i;
            System.out.print(arr2[r][c]);
            if(i<91){
                i++;
            }
        }
        System.out.println();
    }

I kinda understand why the first doesn't work, but I still want to know the optimal way to go about this.

  • 1
    If you understand why your first attempt isn't working, then why do you have it in your question? https://stackoverflow.com/help/how-to-ask states the following: "Pretend you're talking to a busy colleague and have to sum up your entire question in one sentence" – FailingCoder Oct 17 '19 at 20:56

1 Answers1

0

Consider using the alphabet as the loop and computing the destination rather

public static void main(String[ ] args) {
    char[][] alphabet = new char[4][7];
    char c = 'a';
    for (int i = 0; i < 26; i++) {
        alphabet[i/7][i%7] = c++;
    }

    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 7; j++) {
            System.out.print(alphabet[i][j]);
        }
        System.out.println();
    }
}
bradm6s
  • 102
  • 5