Sign up ×
Stack Overflow is a community of 4.7 million programmers, just like you, helping each other. Join them; it only takes a minute:

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.

share|improve this question

closed as off-topic by Alexander Pogrebnyak, Brian Roach, Richard Sitze, skuntsel, Michael Härtl Aug 10 '13 at 6:49

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist" – Richard Sitze, skuntsel, Michael Härtl
If this question can be reworded to fit the rules in the help center, please edit the question.

5  
Hmmm what error? (it throws an exception? it does not calculate the correct combinations? ...) – c.s. Aug 9 '13 at 18:23
4  
26^n is quite large; even for an n as small as 5 it becomes 11881376. You can't even go past n = 6 because you can't create an array that large (the value surpasses int's max value). – arshajii Aug 9 '13 at 18:23
    
yes what is the error? what kinds of values of n are you trying? Won't take a big n for your int to be insufficient storage – d'alar'cop Aug 9 '13 at 18:27
    
Yes, the exception is ArrayIndexOutofBoundsException. So, how can I create on ordered list of the 26^n words? – boris Aug 9 '13 at 18:28
1  
for what do you need this? for brute-force-hacking? – mmirwaldt Aug 9 '13 at 18:35

2 Answers 2

up vote 1 down vote accepted

I'd suggest using recursion instead of normal iteration; code is much more clear and shorter - it's easier to find bugs.

String[] alphabet = { "a", "b", "c", "d", "e", "f", "g",
"h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s", "t",
"u", "v", "w", "x", "y", "z" };
final int amount = 26; //alphabet.size()
StringBuffer[] foo = new StringBuffer[pow(amount, n)];
long counter = 0;

public void recursion(int depth, StringBuffer actual)
{
    String a = actual.toString();
    for (int i = 0; i < amount; i++)
    {
        foo[counter++] = new StringBuffer(a + alphabet[i]);
        if (depth != 1)
            recursion(depth - 1, new StringBuffer(a + alphabet[i]));
    }
}

And just call recursion(n, "").

share|improve this answer

In your last for:

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

You are trying to access your array with a indexbigger than your array. You may not have realized but your int x became bigger than your array precisely here:

x =pow(26, n-h+1)*u + pow(26, n-h)*j

EDIT:

As @d'alar'cop said in his comment: your mathematical operation is faulty

share|improve this answer
2  
you should probably say why exactly his expression pushes x out of range... ie what mathematical operation is faulty.. and why, ideally – d'alar'cop Aug 9 '13 at 18:32
    
I thought it was pretty obvious, sorry and thanks – Diego Rodrigues Aug 9 '13 at 18:34
    
@drurenia but when h = 1, u = 0 – boris Aug 9 '13 at 22:48
    
I have controlled my method, the mathematical part is correct because now it works; It was a heap problem. – boris Aug 17 '13 at 9:23

Not the answer you're looking for? Browse other questions tagged or ask your own question.