for loop - In Java, why do we need to subtract some numbers in boolean expression? -


i'm having difficulty in figuring out why subtract numbers in boolean expression part of loop.

example 1: goal count numbers of same 2 length long substrings of each string.

public int stringmatch(string a, string b) {  int len = math.min(a.length(), b.length());  int count = 0;  for(int c = 0; c < len-1; c++) {  string sub1 = a.substring(c, c + 2);  string sub2 = b.substring(c, c + 2);   if(sub1.equals(sub2))  count++; }  return count; } 

example 2: goal return true if array contains {1, 2, 3}(in order) indexes.

public boolean array123(int[] nums) {   for(int = 0; < nums.length -2; a++)  {     if(nums[a] == 1 && nums[a+1] == 2 && nums[a+2] == 3)    return true;  }   return false;  } 

array indices start @ 0. if have array has 10 values stored in first value stored in array[0] second in array[1] way array[9].

int len = math.min(a.length(), b.length()) 

sets len variable number of characters in whichever string shorter (a or b).

so if has 10 characters , b has 11 characters output a's length , set value of len variable 10.

all strings in essence arrays of characters. remember arrays start @ 0. there being 10 characters in string, when loop through each character have start @ 0 , go 9. len set value of 10... index 10 not exist in string a. last index in string index 9.

normally in loop in order iterate through each element in array have start 0 (int c = 0;). continuing example, want start @ 0 , go 9 in order loop through each element in string. that's why in loop can put -1...as in example don't want try access element @ index 10 cause not exist.

however, there ways around using -1....it not mandatory. instance let's @ code:

for(int c = 0; c < len-1; c++) 

c < len-1; not correct if want loop through , every character of string. continuing our example. have 10 characters in our string. way loop set ever 9 of 10 characters (we loop 9 times). c < len-1, len = 10; 10 - 1 = 9, c < 9 means loop can iterate while c has value between 0 , 8 (c less 9)...that's 9 iterations , need 10 every character of 10 character string. can take out -1 , loop through each character in string. c < len; iterate while c has value between 0 , 9...once c hits value of 10 no longer less value of len, equal it. therefore, stop looping.

now, if wanted use -1. have used c <= len-1; program loop until value of c no longer less or equal value of len-1 (9). once c hits value 10 stop looping.

now in code in first loop creating substrings index @ value c index @ value c + 2:

 string sub1 = a.substring(c, c + 2);  string sub2 = b.substring(c, c + 2); 

because of using c < len-1; have out of bounds error. when 9th character of 10 character string run problem. trying create substring 9th character stored @ index 8 11th character stored @ index 10....oops doesnt exist. take care of if statement:

if((c+2) > 10) {    break; } 

this 1 way handle this.

you use c < len - 2; loop 7th index (where 8th character stored) , create substring index 9th index (where 10th character stored).


Comments

Popular posts from this blog

javascript - Jquery show_hide, what to add in order to make the page scroll to the bottom of the hidden field once button is clicked -

javascript - Highcharts multi-color line -

javascript - Enter key does not work in search box -