A faster way to split a string in java and then add to an ArrayList

For a school project, I was asked to write a simple mathematical parser in Java. The program works great. So great that I used the NetBeans profiling tool to test program performance. To do this, I made a cycle of 1000 calls of the mathematical analyzer of the following expression: "1-((x+1)+1)*2"where I xwas replaced by the current cycle counter. It took 262ms. The fact is that it took 50% of the time in the splitFormula method, which I will give below:

private static void splitFormula(String formula){
    partialFormula=new ArrayList<>();

    for(String temp: formula.split("\\+|\\-|\\*|\\/"))
        partialFormula.add(temp);
}

where partialFormula is ArrayListstrings. To quantify the expression, I need to call the splitFormula method at different times, so I really need to clear the contents of the partial row ArrayList- the first row.

My question is: is there a faster way to split the line and then add partial lines to the arraylist? Or is there some other method that can be used to split the string and then use substrings?

+4
source share
4 answers

(String#split ). , , , , , . :

: ( StringBuilder):

private static void splitFormula(String formula){
    partialFormula.clear(); // since there is a method for this, why not use it?

    int lastIndex = 0;
    for (int index = 0; index < formula.length(); index++) {
        char c = formula.charAt(index);
        if (c == '-' || c == '+' || c == '*' || c == '/') {
            partialFormula.add(formula.substring(lastIndex, index));
            lastIndex = index + 1; //because if it were index, it would include the operator
        }
    }
    partialFormula.add(formula.substring(lastIndex));
}

StringBuilder:

private static void splitFormula(String formula){
    partialFormula.clear();

    StringBuilder newStr = new StringBuilder();

    for (int index = 0; index < formula.length(); index++) {
        char c = formula.charAt(index);
        if (c == '-' || c == '+' || c == '*' || c == '/') {
            partialFormula.add(newStr.toString());
            newStr.setLength(0);
        } else {
            newStr.append(c);
        }
    }
    partialFormula.add(newStr.toString());
}

String#split, , ( GrepCode):

public String[] split(String regex, int limit) {
    return Pattern.compile(regex).split(this, limit);
}

! , , , , Pattern#split :

//In constructor, or as a static variable.
//This regex is a better form of yours.
Pattern operatorPattern = Pattern.compile("[-*+/]");
...
private static void splitFormula(String formula){
    partialFormula.clear();

    for(String temp: operatorPattern.split(formula)) {
        partialFormula.add(temp);
    }
}
+7

for. split , ArrayList :

partialFormula = new ArrayList<>(Arrays.asList(formula.split("\\+|\\-|\\*|\\/")));

, .

0

Try pre-allocating an ArrayList in advance, so we don’t need to pay for redistribution when the list grows. The number 20below is just a placeholder. Choose a number that is slightly larger than the largest expression you expect.

partialFormula=new ArrayList<String>(20);

See this question for a discussion of how it can benefit you.

0
source

This will create an array of strings from strings.

String a= "1234+af/d53";
    char [] blah=a.toCharArray(); 
    ArrayList<String> list=new ArrayList<String>();
    for (int i = 0; i < blah.length; i++) {
        list.add(Character.toString(blah[i]));  
    }
0
source

Source: https://habr.com/ru/post/1536512/


All Articles