commons-issues mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Krzysztof Nazarewski (JIRA)" <>
Subject [jira] [Commented] (LANG-810) StringUtils.join() endIndex, bugged for loop
Date Wed, 11 Jul 2012 08:41:34 GMT


Krzysztof Nazarewski commented on LANG-810:

Basically endIndex takes values from 1 to array.length instead of from 0 to (array.length
- 1) and is not really index at all.

If you put valid index 0, the for loop won't run at all as it checks (i < endIndex) like
it was array's length.

Let's say you have 1 element array to join (it does not make practical sense, bu should work),
it has index 0 and does not have anything beyond 0 index. 
According to JavaDoc only possible combination of startIndex and endIndex would be (0,0)., line 3394: {{for (int i = startIndex; i < endIndex; i++) {}}
However for loop will not execute even single time because {{(i = startIndex = 0)}} is not
lower than {{(endIndex = 0)}}, it is equal to endIndex and the result of joining single element
array from it's only index 0 to index 0 will be empty string.

Therefore to join that one-element array you need to use combination {{(startIndex = 0, endIndex
= 1)}}, where {{(endIndex == array.length)}} is error according to JavaDoc:
"endIndex - the index to stop joining from (exclusive). *It is an error to pass in an end
index past the end of the array*"

> StringUtils.join() endIndex, bugged for loop
> --------------------------------------------
>                 Key: LANG-810
>                 URL:
>             Project: Commons Lang
>          Issue Type: Bug
>          Components: lang.*
>    Affects Versions: 3.1
>            Reporter: Krzysztof Nazarewski
>            Assignee: Joerg Schaible
>   Original Estimate: 1m
>  Remaining Estimate: 1m
> endIndex is described as index, but for loop still checks it as "array length".
> Basically missing equal sign
> commons-lang3-3.1-sources.jar, lines 3309, 3394:
>         for (int i = startIndex; i < endIndex; i++) {
> should be:
>         for (int i = startIndex; i <= endIndex; i++) {

This message is automatically generated by JIRA.
If you think it was sent incorrectly, please contact your JIRA administrators:!default.jspa
For more information on JIRA, see:


View raw message