View this PageEdit this PageAttachments to this PageHistory of this PageHomeRecent ChangesSearch the SwikiHelp Guide

Incorrect looping

Be sure to loop through all of an array or list. This is often easiest with the for-each loop.

The following code will miss the last item in the array.
for (int i = 0; i < intArray.length - 1; i++)
{
}


To loop through all elements in an array use:
for (int i = 0; i < intArray.length; i++)
{
}


or

for (int i = 0; i <= intArray.length - 1; i++)
{
}


To loop through all elements of a list do:
for (String str : strList)

or
String str = null;
for (int i = 0; i < strList.size(); i++)
{
   str = strList.get(i);
}



or for an array of objects (like Students) use:
for (Student currStudent : studentArray)
{
}

Links to this Page