Joshua Lochner
Joshua Lochner

Reputation: 153

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

Hello fellow Stackoverflowers. I have been playing around with arrays in java, and I have been trying to store huge amounts of values in an array. However, I've not been able to store more than a certain amount of values in an array:

String data[] = new String[44681003];//For some reason 44681003 is the highest number I can go to until it spits out an ugly red error message through the console:

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

I have a program which generates all the permutations of a given string list, and it works perfectly until I have to generate a number greater than that strange 44680815 number. (For example: 387420489 which is 9^9)

I have tried storing the value and printing it out to the console in a for loop, set the value back to null data[i] = null;

I was just wondering whether there is a way to store larger amounts of values in an array?

OR

Being able to simply print out my value and then remove it from being stored in the array.


Here is my code:

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



    }

}

Last lines in console:

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


Thank you for your time! I hope I can find a solution and maybe help other people with the same/similar question!

Upvotes: 0

Views: 2510

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533442

If you are getting an OutOfMemoryError your program needs more memory to do what you are asking it to do

However, since you don't retain any of the strings you store in the array i.e.

data[i] = null;

I suggest you remove the array as you don't need it. This will solve your memory problem.

You could turn your code into a function so that you don't have to build an array even if you need random access later.

BTW You get N! permutations from a set of N as you cannot repeat any letters. e.g. badagahcc is not a permutation as it has a 3 times and c twice.

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;
}

This way you can get any permutation without memorization.

Upvotes: 3

Related Questions