My goal is to create a 2d array of the alphabet like this:
abc
bca
cab
Such that each row and column has all 26 letters. I know I can do this the manual ugly way, but was hoping someone knows a more efficient way to populate the array.
Thank you.
Here you go.
class Alphabet {
public static void main(String args[]) {
// Create an array that will hold the grid
char alphGrid[][] = genArray();
// Two for loops to print the grid on the screen
for(int i=0; i<26; i++) {
for(int j=0; j<26; j++) {
System.out.print(alphGrid[i][j]);
}
System.out.println();
}
} // end of main
// Create a function to generate the grid
public static char[][] genArray(){
char[][] arr = new char[26][26];
// Two for loops to generate the grid
for(int i = 0; i < 26; i++) {
for(int j = 0; j < 26; j++) {
// Creates an int that will later be cast to a char
int let = i + j;
// Keeps the int from getting too big
if(let >= 26)
let = let - 26;
// Add 65 to the int so that the char will return letters and not ASCII symbols
let = let + 65;
// Cast the int to a char
char letter = (char)let;
// Put the char into its respective place in the array
arr[i][j] = letter;
}
}
// Returns the grid
return arr;
}
}
Output:
ABCDEFGHIJKLMNOPQRSTUVWXYZ
BCDEFGHIJKLMNOPQRSTUVWXYZA
CDEFGHIJKLMNOPQRSTUVWXYZAB
DEFGHIJKLMNOPQRSTUVWXYZABC
EFGHIJKLMNOPQRSTUVWXYZABCD
FGHIJKLMNOPQRSTUVWXYZABCDE
GHIJKLMNOPQRSTUVWXYZABCDEF
HIJKLMNOPQRSTUVWXYZABCDEFG
IJKLMNOPQRSTUVWXYZABCDEFGH
JKLMNOPQRSTUVWXYZABCDEFGHI
KLMNOPQRSTUVWXYZABCDEFGHIJ
LMNOPQRSTUVWXYZABCDEFGHIJK
MNOPQRSTUVWXYZABCDEFGHIJKL
NOPQRSTUVWXYZABCDEFGHIJKLM
OPQRSTUVWXYZABCDEFGHIJKLMN
PQRSTUVWXYZABCDEFGHIJKLMNO
QRSTUVWXYZABCDEFGHIJKLMNOP
RSTUVWXYZABCDEFGHIJKLMNOPQ
STUVWXYZABCDEFGHIJKLMNOPQR
TUVWXYZABCDEFGHIJKLMNOPQRS
UVWXYZABCDEFGHIJKLMNOPQRST
VWXYZABCDEFGHIJKLMNOPQRSTU
WXYZABCDEFGHIJKLMNOPQRSTUV
XYZABCDEFGHIJKLMNOPQRSTUVW
YZABCDEFGHIJKLMNOPQRSTUVWX
ZABCDEFGHIJKLMNOPQRSTUVWXY