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;
0 responses so far ↓
There are no comments yet...Kick things off by filling out the form below.
Leave a Comment