Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

String concatenation in loop. Java

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.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel


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

>Solution :

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

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading