J2ME: Bounds checking of a List
Thursday, 8 May 2008
This short routine shows how to ensure a selected item in a list is within bounds. If the list is empty the getSelectedIndex will return -1. The index starts at zero whilst size() will start at 1.
// perform bounds checking
int iIndex = myList.getSelectedIndex();
int iCount = myList.size();
// debugging information
System.out.println("Index: " + iIndex + " Size: " + iCount);
// ensure choice is within bounds
if ( iIndex < 0 || iIndex >= iCount) return;

Jason Slater is an independent technologist and blogger.