jira.codehaus.org

  • Log In Access more options
    • Online Help
    • Keyboard Shortcuts
    • About JIRA
    • JIRA Credits
    • What?s New
  • Dashboards Access more options (Alt+d)
  • Projects Access more options (Alt+p)
  • Issues Access more options (Alt+i)
  • groovy
  • GROOVY-1286

Inconsistent behavior between positive indices and negative indices.

  • Log In
  • Views
    • XML
    • Word
    • Printable

Details

  • Type: Bug Bug
  • Status: Closed Closed
  • Priority: Major Major
  • Resolution: Won't Fix
  • Affects Version/s: 1.0-JSR-5
  • Fix Version/s: 1.5
  • Component/s: None
  • Labels:
    None
  • Environment:
    WinXP SP2
  • Testcase included:
    yes

Description

------------------------------------------------------------
def list = [1,2,3,4,5,6,7,8,9]

println list[99] // null is returned
println list[-99] // throws java.lang.ArrayIndexOutOfBoundsException
------------------------------------------------------------
Stack trace:
Caught: java.lang.ArrayIndexOutOfBoundsException: Negative array index [-99] too large for array size 9
at test.run(test.groovy:5)
at test.main(test.groovy)

To me, this seems to be inconsistent behavior for two similar expressions.

Activity

Ascending order - Click to sort in descending order
  • All
  • Comments
  • Work Log
  • History
  • Activity
Hide
Permalink
Guillaume ALLEON added a comment - 01/Apr/06 12:20 PM

the same method normaliseIndex is used both for putAt & getAt. An ArrayIndexOutOfBoundsException
have to be thrown for getAt when index is greater than size.

Show
Guillaume ALLEON added a comment - 01/Apr/06 12:20 PM the same method normaliseIndex is used both for putAt & getAt. An ArrayIndexOutOfBoundsException have to be thrown for getAt when index is greater than size.
Hide
Permalink
Andres Almiray added a comment - 03/Dec/07 10:03 PM

The behavior can be explained as follows:

lists/collections:

  • negative index can't be greater that collection size
  • positive index may be greater that collection size, yielding null because of
    def list = []
    list[100] = "ok"
    assert list[50] == null
    assert list.size() == 101
    assert list[-100] == null
    try{ list[-101] == null }
    catch( ArrayIndexOutOfBoundsException expected ) { /*ok!*/}

arrays:

  • negative and positive index can't be greater than the size of the array, as arrays have a fixed length

strings:

  • negative and positive index can't be greater than the size of the string as strings have a fixed length

Because this issue was reported against Lists I think it can be closed as "Not an Issue".

Show
Andres Almiray added a comment - 03/Dec/07 10:03 PM The behavior can be explained as follows: lists/collections:
  • negative index can't be greater that collection size
  • positive index may be greater that collection size, yielding null because of
    def list = []
    list[100] = "ok"
    assert list[50] == null
    assert list.size() == 101
    assert list[-100] == null
    try{ list[-101] == null }
    catch( ArrayIndexOutOfBoundsException expected ) { /*ok!*/}
arrays:
  • negative and positive index can't be greater than the size of the array, as arrays have a fixed length
strings:
  • negative and positive index can't be greater than the size of the string as strings have a fixed length
Because this issue was reported against Lists I think it can be closed as "Not an Issue".
Hide
Permalink
Marc Palmer added a comment - 25/Oct/10 8:52 AM

In hindsight, is it not a slightly odd decision to go with this behaviour?

Take the following output from groovy console:

groovy> def o = [name:'A'] 
groovy> def lineage = [o] 
groovy> Object[] lineageArray = [o] 
groovy> def index = 0 
groovy> println lineage[index++] 
groovy> println lineage[index++] 
groovy> index = 0 
groovy> println lineageArray[index++] 
groovy> println lineageArray[index++] 
 
[name:A]
null
[name:A]
Exception thrown
Oct 25, 2010 2:29:11 PM org.codehaus.groovy.runtime.StackTraceUtils sanitize
WARNING: Sanitizing stacktrace:
java.lang.ArrayIndexOutOfBoundsException: 1

Did we really want this divergent behaviour? Surely getAt on lists should throw AIOOB but putAt shouldn't.

Show
Marc Palmer added a comment - 25/Oct/10 8:52 AM In hindsight, is it not a slightly odd decision to go with this behaviour? Take the following output from groovy console:
groovy> def o = [name:'A'] 
groovy> def lineage = [o] 
groovy> Object[] lineageArray = [o] 
groovy> def index = 0 
groovy> println lineage[index++] 
groovy> println lineage[index++] 
groovy> index = 0 
groovy> println lineageArray[index++] 
groovy> println lineageArray[index++] 
 
[name:A]
null
[name:A]
Exception thrown
Oct 25, 2010 2:29:11 PM org.codehaus.groovy.runtime.StackTraceUtils sanitize
WARNING: Sanitizing stacktrace:
java.lang.ArrayIndexOutOfBoundsException: 1
Did we really want this divergent behaviour? Surely getAt on lists should throw AIOOB but putAt shouldn't.
Hide
Permalink
blackdrag blackdrag added a comment - 25/Oct/10 10:27 AM

your example does not contain a putAt that throws an exception.

Show
blackdrag blackdrag added a comment - 25/Oct/10 10:27 AM your example does not contain a putAt that throws an exception.

People

  • Assignee:
    Unassigned
    Reporter:
    Ryan Misek
Vote (0)
Watch (0)

Dates

  • Created:
    31/Mar/06 2:26 PM
    Updated:
    25/Oct/10 10:27 AM
    Resolved:
    03/Dec/07 10:03 PM
  • Atlassian JIRA (v5.0.4#731-sha1:3aa7374)
  • Report a problem
  • Powered by a free Atlassian JIRA open source license for Codehaus. Try JIRA - bug tracking software for your team.