I'm trying to create an string arraylist with a size 100,000, wherein every element after the first two is the previous two strings concatenated. The problem is, I run out of memory before I even reach 100 strings. I plan to use this arraylist to do a binary search and output a letter in the string based on the input given, which is the index of the string, and the index of the letter within the string.
Here's the code
public static void generateSequence(int numStrings, ArrayList<String> input){
String temp = "", S1, S2;
for(int i = 0; i < n; i++) {
if(i == 0) {
input.add("H");
}
else if(i == 1) {
input.add("A");
}
else {
S1 = input.get(input.size() - 2);
S2 = input.get(input.size() - 1);
temp = S1.concat(S2);
input.add(temp);
temp = "";
}
}
}
I'm not sure how to be more efficient with the memory usage to get the arraylist I need for this, so any advice would be helpful. Thank you.