Java - 如何在数组中存储大量值

Java - How to store huge amounts of values in an array

大家好 Whosebugers。我一直在玩弄 java 中的数组,并且一直试图在数组中存储大量值。但是,我无法在数组中存储超过一定数量的值:

String data[] = new String[44681003];//出于某种原因,44681003 是我可以访问的最高数字,直到它通过控制台吐出丑陋的红色错误消息:

Exception in thread "main" java.lang.OutOfMemoryError: Java heap space

我有一个程序可以生成给定字符串列表的所有排列,它可以完美运行,直到我必须生成一个大于那个奇怪数字 44680815 的数字。 (例如:387420489 即 9^9)

我已经尝试存储该值并在 for 循环中将其打印到控制台,将值设置回 null data[i] = null;

我只是想知道是否有办法在数组中存储更多的值?

能够简单地打印出我的值,然后将其从存储在数组中删除。


这是我的代码:

public class Permutations {

public static void main(String[] args) {
    String database = "abcdefghi";

//  String data[] = new String[(int) Math.pow(database.length(), database.length())];
// ^^ I would like to make it this long, but It gives an error.

    String data[] = new String[44681003];
    StringBuilder temp;


    for (int i = 0;i<Math.pow(database.length(), database.length());i++){
        String base = Integer.toString(i,database.length());
        data[i] = base;
        if (base.length()!=database.length()){
             temp = new StringBuilder(""); 
            for (int x = 0;x < (database.length()-data[i].length());x++){
                temp.append('0');
            }
            base = temp + base;

        }

        for (int y = 0;y<database.length();y++){
            base = base.replace((char)('0' + y), database.charAt(y));
        }

        data[i]=null;

        System.out.println("Pos: " + i + "     " + base); //<-- USE THIS TO WRITE IT OUT
    }//end big for loop
    System.out.println("Done");



    }

}

控制台中的最后几行:

Pos: 44680997     badagahcc
Pos: 44680998     badagahcd
Pos: 44680999     badagahce
Pos: 44681000     badagahcf
Pos: 44681001     badagahcg
Pos: 44681002     badagahch
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 44681003
at Permutations.main(Permutations.java:20)

My computer specs: http://store.asus.com/us/item/201510AM160007799/A17602


感谢您的宝贵时间!我希望我能找到解决方案,也许可以帮助其他人解决 same/similar 问题!

如果您收到 OutOfMemoryError,您的程序需要更多内存才能执行您要求它执行的操作

但是,由于您不保留存储在数组中的任何字符串,即

data[i] = null;

我建议您删除阵列,因为您不需要它。这将解决您的记忆问题。

您可以将您的代码变成一个函数,这样即使您以后需要随机访问也不必构建数组。

顺便说一句,你得到了 N!一组 N 的排列,因为您不能重复任何字母。例如badagahcc 不是排列,因为它有 a 3 次和 c 两次。

public static String generate(String letters, long number) {
    // get a list of the all the possible characters assuming no duplicates.
    List<Character> chars = new ArrayList<>(letters.length());
    for (int i = 0; i < letters.length(); i++)
        chars.add(letters.charAt(i));
    // start with a string builder.
    StringBuilder ret = new StringBuilder(letters.length());

    // while we have characters left
    while(chars.length() > 0) {
       // select one of the unused characters
       int select = number % chars.length();
       // take out the lower portion of the number and use the next portion
       number /= chars.length();
       // Append the N-th character, but remove it so it doesn't get used again.
       ret.append(chars.remove(select));
    }
    assert number == 0; // otherwise we have not enough letters.
    return ret;
}

这样你不用背就能得到任意排列。