Java,创建一个StringBuffer数组

Java,创建一个StringBuffer数组,java,arrays,sorting,Java,Arrays,Sorting,我对Java有一个问题 假设n是一个整数,我想创建一个StringBuffer数组,其中包含字母表中所有26^n个字母的组合,并按字典顺序排列。我获得ArrayIndexOutOfBoundsException 我写了这节课: public static StringBuffer[] creaTabellaTotale(int n, char[] a) { StringBuffer[] tabella = new StringBuffer[ pow(26, n)];

我对Java有一个问题

假设n是一个整数,我想创建一个StringBuffer数组,其中包含字母表中所有26^n个字母的组合,并按字典顺序排列。我获得ArrayIndexOutOfBoundsException

我写了这节课:

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;
 }
publicstaticstringbuffer[]creaTabellaTotale(intn,char[]a){
StringBuffer[]tabella=新的StringBuffer[pow(26,n)];
对于(intw=0;wfor中:

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]);
编辑:

正如@d'alar'cop在他的评论中所说的那样:你的数学运算在你上一次的中是错误的

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]);
编辑:


正如@d'alar'cop在他的评论中所说的:你的数学运算是错误的

我建议使用递归而不是普通的迭代;代码更清晰、更短——更容易发现bug

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]));
    }
}
String[]字母表={“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=新StringBuffer[pow(金额,n)];
长计数器=0;
公共void递归(int-depth,StringBuffer-actual)
{
字符串a=实际的.toString();
对于(int i=0;i

只要调用
递归(n,””

我建议使用递归而不是普通的迭代;代码更清晰、更短,更容易发现bug

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]));
    }
}
String[]字母表={“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=新StringBuffer[pow(金额,n)];
长计数器=0;
公共void递归(int-depth,StringBuffer-actual)
{
字符串a=实际的.toString();
对于(int i=0;i

只需调用递归(n,“”

Hmmm什么错误?(它抛出异常?它没有计算正确的组合?…)
26^n
相当大;即使对于小于
5
n
也会变成
11881376
。您甚至无法通过
n=6
,因为您无法创建这么大的数组(该值超过int的最大值).是的,错误是什么?你在尝试什么类型的n值?不会用一个大的n表示你的int不足以存储是的,例外是ArrayIndexOutofBoundsException。那么,我如何在26^n个单词的有序列表上创建?你需要什么?暴力破解?嗯,什么错误?(它抛出了一个异常?它没有计算正确的组合?…)
26^n
相当大;即使对于小于
5
n
也会变成
11881376
。你甚至不能通过
n=6
,因为你无法创建一个那么大的数组(值超过int的最大值).是的,错误是什么?你在尝试什么类型的n值?不会用一个大的n表示你的int不足以存储是的,例外是ArrayIndexOutofBoundsException。那么,我如何在26^n单词的有序列表上创建呢?你需要什么?对于暴力破解?你可能应该说为什么他的表达式将x推出of范围(什么数学运算有问题)以及为什么,我认为这很明显,对不起thanks@drurenia但是当h=1,u=0时,我已经控制了我的方法,数学部分是正确的,因为现在它工作了;这是一个堆问题。你可能应该说,为什么他的表达式将x推到了范围之外…即mathematical的操作有问题,我想这是很明显的,抱歉thanks@drurenia但是当h=1,u=0时,我控制了我的方法,数学部分是正确的,因为现在它工作了;这是一个堆问题。