accessing array in loop code example
Example 1: arrays with for loops
// Find-Max variant
// Use very small number, Integer.MIN_VALUE,
// as the initial max value.
public int findMax2(int[] nums) {
int maxSoFar = Integer.MIN_VALUE; // about -2 billion
// Look at every element
for (int i=0; i maxSoFar) {
maxSoFar = nums[i];
}
}
return maxSoFar;
}
Example 2: arrays with for loops
// Another way to write search that combines
// the "end of array" and "found" logic in one
// while loop. As a matter of style, we prefer
// the above version that uses the standard
// for-all loop.
public int searchNotAsGood(int[] nums, int target) {
int i = 0;
while (i