I have a problem with Java.
Suppose n is an integer, I want to create an array of StringBuffer containing all the 26^n combinations of letters of the alphabet, with the lexicographic order. I obtain ArrayIndexOutOfBoundsException.
I wrote this class:
public static StringBuffer[] creaTabellaTotale(int n, char[] a) {
StringBuffer[] tabella = new StringBuffer[ pow(26, n)];
for(int w =0; w < pow(26, n); w++)
tabella[w] = new StringBuffer("");
for(int h = 1; h <= n ; h++){
for(int u =0; u < pow ( 26, h-1); u++){
for (int j = 0; j<26; j++){
for( int x =pow(26, n-h+1)*u + pow(26, n-h)*j; x< pow(26, n-h+1)*u + pow(26, n-h)*(j+1); x++)
tabella[x] = tabella[x].append(a[j]);
}
}
}
return tabella;
}
Here a[] is an array containing the 26 letters in alphabetic order; I have rewritten pow(), its prototype is int pow(int b, int e). I'm not able to find the error.
26^n
is quite large; even for ann
as small as5
it becomes11881376
. You can't even go pastn = 6
because you can't create an array that large (the value surpasses int's max value). – arshajii Aug 9 '13 at 18:23