-
Bug
-
Resolution: Not an Issue
-
P3
-
None
-
6u14
-
None
FULL PRODUCT VERSION :
1.6.0.14
ADDITIONAL OS VERSION INFORMATION :
Windows 7 32 Bit
EXTRA RELEVANT SYSTEM CONFIGURATION :
Write a simple java program use
List list = new ArrayList();
call list.get(-1); //Case 1
call list.get(1); // Case 2
In Case 1 It throws ArrayIndexOutOfBoundException.
In Case 2 It throws IndexOutOfBoundException.
Due to rangeCheck() method implementation the condition is
If(index>=size) {
throw new IndexOutOfBoundException();
}
it should check for index<0 as well in the same condition as this check is already there in a method called rangeCheckForAdd() // See java.util.ArrayList.java for more details
A DESCRIPTION OF THE PROBLEM :
The Java Docs for the get(index) API for java.util.List and java.util.ArrayList
shows that if index is less than 0 or greater than or equals to list's size it will thorw IndexOutOfBoundException.
However when we call the get(index) method with index =-1 it throws ArrayIndexOutOfBoundException and if call with index is greater than or equals to List's size it throws IndexOutOfBoundException.
It's not a major bug however I still feel that the rangeCheck(index) method of java.util.ArrayList should be updated with condition
// should be (index > size || index < 0) as it is in rangeCheckForAdd(index);
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
REGRESSION. Last worked in version 6u31
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
run the below program.
import java.util.ArrayList;
import java.util.List;
/**
*
*/
/**
* @author Administrator
*
*/
public class TestList {
/**
*
*/
public TestList() {
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) {
List list = new ArrayList();
list.get(1);
//list.get(-1);
}
}
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Case 1. list.get(1); throws
Exception in thread " main " java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at TestList.main(TestList.java:27)
Case 2. list.get(-1); // Throws
Exception in thread " main " java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.get(ArrayList.java:324)
at TestList.main(TestList.java:28)
ACTUAL -
I know that ArrayIndexOutOfBoundsException is a subclass of IndexOutOfBoundException.
However when you look at the java doc for the API java.util.List.get(index) it will show you this:
Throws:
IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
it should do as the API suggested and implement the same condition written in API in rangeCheck(int index) method or should update the API.
ERROR MESSAGES/STACK TRACES THAT OCCUR :
different error message in Test-case 1 and Test-case 2
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.util.ArrayList;
import java.util.List;
/**
*
*/
/**
* @author Administrator
*
*/
public class TestList {
/**
*
*/
public TestList() {
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) {
List list = new ArrayList();
list.get(1); //TEST CASE - 1
//list.get(-1); //TEST CASE - 2
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
you have to provide the same condition check in your application code which will throw the same exception before using the List.get(index) API.
SUPPORT :
YES
1.6.0.14
ADDITIONAL OS VERSION INFORMATION :
Windows 7 32 Bit
EXTRA RELEVANT SYSTEM CONFIGURATION :
Write a simple java program use
List list = new ArrayList();
call list.get(-1); //Case 1
call list.get(1); // Case 2
In Case 1 It throws ArrayIndexOutOfBoundException.
In Case 2 It throws IndexOutOfBoundException.
Due to rangeCheck() method implementation the condition is
If(index>=size) {
throw new IndexOutOfBoundException();
}
it should check for index<0 as well in the same condition as this check is already there in a method called rangeCheckForAdd() // See java.util.ArrayList.java for more details
A DESCRIPTION OF THE PROBLEM :
The Java Docs for the get(index) API for java.util.List and java.util.ArrayList
shows that if index is less than 0 or greater than or equals to list's size it will thorw IndexOutOfBoundException.
However when we call the get(index) method with index =-1 it throws ArrayIndexOutOfBoundException and if call with index is greater than or equals to List's size it throws IndexOutOfBoundException.
It's not a major bug however I still feel that the rangeCheck(index) method of java.util.ArrayList should be updated with condition
// should be (index > size || index < 0) as it is in rangeCheckForAdd(index);
private void rangeCheck(int index) {
if (index >= size)
throw new IndexOutOfBoundsException(outOfBoundsMsg(index));
}
REGRESSION. Last worked in version 6u31
STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
run the below program.
import java.util.ArrayList;
import java.util.List;
/**
*
*/
/**
* @author Administrator
*
*/
public class TestList {
/**
*
*/
public TestList() {
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) {
List list = new ArrayList();
list.get(1);
//list.get(-1);
}
}
EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Case 1. list.get(1); throws
Exception in thread " main " java.lang.IndexOutOfBoundsException: Index: 1, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.get(ArrayList.java:322)
at TestList.main(TestList.java:27)
Case 2. list.get(-1); // Throws
Exception in thread " main " java.lang.ArrayIndexOutOfBoundsException: -1
at java.util.ArrayList.get(ArrayList.java:324)
at TestList.main(TestList.java:28)
ACTUAL -
I know that ArrayIndexOutOfBoundsException is a subclass of IndexOutOfBoundException.
However when you look at the java doc for the API java.util.List.get(index) it will show you this:
Throws:
IndexOutOfBoundsException - if the index is out of range (index < 0 || index >= size())
it should do as the API suggested and implement the same condition written in API in rangeCheck(int index) method or should update the API.
ERROR MESSAGES/STACK TRACES THAT OCCUR :
different error message in Test-case 1 and Test-case 2
REPRODUCIBILITY :
This bug can be reproduced always.
---------- BEGIN SOURCE ----------
import java.util.ArrayList;
import java.util.List;
/**
*
*/
/**
* @author Administrator
*
*/
public class TestList {
/**
*
*/
public TestList() {
// TODO Auto-generated constructor stub
}
/**
* @param args
*/
public static void main(String[] args) {
List list = new ArrayList();
list.get(1); //TEST CASE - 1
//list.get(-1); //TEST CASE - 2
}
}
---------- END SOURCE ----------
CUSTOMER SUBMITTED WORKAROUND :
you have to provide the same condition check in your application code which will throw the same exception before using the List.get(index) API.
SUPPORT :
YES