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
- Windows 10
- Intel Core i7 4720HQ 2.6GHz (Turbo up to 3.6GHz)
- 16GB Memory
- 1TB HDD
- NVIDIA GeForce GTX 970M 3GB
- 17.3-inch IPS FHD (1920 x 1080) supported G-Sync
Thank you for your time! I hope I can find a solution and maybe help other people with the same/similar question!
StringBuilder temp;can be declared outside of the loop.