简体   繁体   中英

Java string split method evaluation

I need to parse an input string for an application and have a question related to how java evaluates the result of the string split() method.

For example, in the code bellow:

} else if (arg.equals("-multiplePaths")) {
    // Check if we have multiple paths 
    if (args[count++].contains(":")) {
    for(String tmpIDLPath : args[count-1].split(":"))
        m_includePaths.add(tmpIDLPath);
    } else {
        // Only one
        m_includePaths.add(args[count-1]);
    }

how is for loop evaluated? Is the split operation computed once for each iteration or once at the beginning?

The array you are looping over is computes once per loop.

BTW Your check is redundant.

} else if (arg.equals("-multiplePaths")) {
    for(String tmpIDLPath : args[count-1].split(":"))
        m_includePaths.add(tmpIDLPath);

or

} else if (arg.equals("-multiplePaths")) {
    Collections.addAll(m_includePaths, args[count-1].split(":"));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM