I'm trying to concat string in multiple loop, and I'm having trouble with it.
I tried to do it with "StringBuilder", but it causes memory leak. the only way to wake it work, was to do it with printf function, witch is not logical for me. I thought that StringBuiler or concat() function of String are more efficient way to concatenate strings. Here's example of working code:
public static List<String> generateCoolNumbers() {
final String[] LETTERS = {"А", "В", "Е", "К", "М", "Н", "О", "Р", "С", "Т", "У", "Х"};
List<String> coolNumber = new ArrayList<>();
for (String letterOne : LETTERS) {
for (int number = 111; number <= 999; number = 111) {
for (String letterTwo : LETTERS) {
for (String letterThree : LETTERS) {
for (int region = 1; region < 200; region ) {
if (region <= 9) {
coolNumber.add(String.format("%s%d%s%s0%d", letterOne, number, letterTwo,
letterThree, region));
continue;
}
coolNumber.add(String.format("%s%d%s%s%d", letterOne, number, letterTwo,
letterThree, region));
}
}
}
}
}
return coolNumber;
}
And what is most confusing for me, that code below is not working, because of "Java heap space" exception, instead of being more efficient.
public static List<String> generateCoolNumbers() {
final String[] LETTERS = {"А", "В", "Е", "К", "М", "Н", "О", "Р", "С", "Т", "У", "Х"};
List<String> coolNumber = new ArrayList<>();
StringBuilder strb = new StringBuilder();
for (String letterOne : LETTERS) {
for (int number = 111; number <= 999; number = 111) {
for (String letterTwo : LETTERS) {
for (String letterThree : LETTERS) {
for (int region = 1; region < 200; region ) {
if (region <= 9) {
strb.append(letterOne);
strb.append(number);
strb.append(letterTwo);
strb.append(letterThree);
strb.append("0" region);
coolNumber.add(strb.toString());
continue;
}
strb.append(letterOne);
strb.append(number);
strb.append(letterTwo);
strb.append(letterThree);
strb.append(region);
coolNumber.add(strb.toString());
}
}
}
}
}
return coolNumber;
}
CodePudding user response:
You need to clear or reset the string builder inside your loop :
public static ArrayList<String> generateCoolNumbers() {
final String[] LETTERS = {"A", "B", "E", "K"};
ArrayList<String> coolNumber = new ArrayList<String>();
for (String letterOne : LETTERS) {
for (int number = 111; number <= 999; number = 111) {
for (String letterTwo : LETTERS) {
for (String letterThree : LETTERS) {
for (int region = 1; region < 200; region ) {
StringBuilder strb = new StringBuilder();
if (region <= 9) {
strb.append(letterOne);
strb.append(number);
strb.append(letterTwo);
strb.append(letterThree);
strb.append("0" region);
} else {
strb.append(letterOne);
strb.append(number);
strb.append(letterTwo);
strb.append(letterThree);
strb.append(region);
}
coolNumber.add(strb.toString());
}
}
}
}
}
return coolNumber;
}
you can also use
strb.setLength(0);
CodePudding user response:
You were not appending the strings to list.
public static List<String> generateCoolNumbers() {
final String[] LETTERS = {"А", "В", "Е", "К", "М", "Н", "О", "Р", "С", "Т", "У", "Х"};
List<String> coolNumber = new ArrayList<>();
for (String letterOne : LETTERS) {
for (int number = 111; number <= 999; number = 111) {
for (String letterTwo : LETTERS) {
for (String letterThree : LETTERS) {
for (int region = 1; region < 200; region ) {
StringBuilder strb = new StringBuilder();
if (region <= 9) {
strb.append(letterOne);
strb.append(number);
strb.append(letterTwo);
strb.append(letterThree);
strb.append("0" region);
coolNumber.add(strb.toString());
continue;
}
strb.append(letterOne);
strb.append(number);
strb.append(letterTwo);
strb.append(letterThree);
strb.append(region);
coolNumber.add(strb.toString());
}
}
}
}
}
return coolNumber;
}