I need help with arrays.

From ggc

Jump to: navigation, search

For Array1 > firstLast6 I can get it to return in some cases but not a case in which the array only contains two or less elements. How can I fix this issue. Below is the code I tried.

public boolean firstLast6(int[] nums) {

 for (int i=0; i<(nums.length-1); i++) 
 {
   if (nums[i+0]==6 || nums[i+1]==6) return true;
 }
 return false;

}

Answer

Try something like:

if(nums.length==2)
{
   //do something
}

and so forth for the other cases.





Views
Personal tools
Add to 
del.icio.usAdd to 
diggAdd to 
FacebookAdd to 
favoritesAdd to 
GoogleAdd to 
MySpaceAdd to 
PrintAdd to 
SlashdotAdd to 
StumbleUponAdd to 
Twitter