What is the time complexity of Java StringTokenizer.countTokens()

James Wierzba :

I am hopeful that it has constant time complexity, but the name implies it is actually counting the tokens.

Jacob G. :

Here's the implementation, if you were curious:

public int countTokens() {
    int count = 0;
    int currpos = currentPosition;
    while (currpos < maxPosition) {
        currpos = skipDelimiters(currpos);
        if (currpos >= maxPosition)
            break;
        currpos = scanToken(currpos);
        count++;
    }
    return count;
}

I'm not too familiar with StringTokenizer, but assuming maxPosition can change (which it looks like it can), then it's not constant-time. You also have to take into account the complexities of skipDelimiters and scanToken.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=86168&siteId=1