# Using i-1 in programs

• November 7th, 2012, 12:13 PM
JoshKesner
Using i-1 in programs
Here is the question: Given an array of ints, return the number of times that two 6's are next to each other in the array. Also count instances where the second "6" is actually a 7.
Here is the code:
public int array667(int[] nums) {
int len = nums.length;
int count = 0;
for (int i=0;i<len-1;i++){
if ((nums[i]==6) && (nums[i+1]==6||nums[i+1]==7)){
count++;
}
}
return count;

}
I don't understand why it has to be initialized at i-1 in order to work. Please explain
Also are there any other scenarios in which i-1 would be needed maybe even not for just initializing i.
• November 7th, 2012, 12:29 PM
KevinWorkman
Re: Using i-1 in programs
I don't see an "i-1" in that code at all.
• November 7th, 2012, 02:33 PM
Actinistia
Re: Using i-1 in programs
I don't see it as well but if you meant the "len-1" in your for loop, then it could easily be explained. First of all, you aren't initializing "len-1" but rather setting up a certain condition for the for loop. The reason you need the "-1" is because "len" is the length of the entire array. It seems that you understand that arrays always start at index [0], since the .length() function returns the size of the entire array, you need to compensate with a "-1" in the condition, such that the array doesn't go out of bounds. Say you create an array with the size 10, an array will be created with the array index[0] to array[9]. The length() function would return 10 but you would have to use "-1" in your for loop because array[10] doesn't exist. Sorry if this is a bit confusing.

I'd recommend reading: Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)
• November 7th, 2012, 05:05 PM
jps
Re: Using i-1 in programs
@Actinistia myArray.length is more of a variable than a function and does not include () in its use.

@JoshKesner A sample program that shows what has been explained:

Code java:

```/** FILE: forumsamples.Arrays_IndexVsSize.java*/ package forumsamples;         /** * @author jps<br> * This class shows the creation of an array of a given number of elements by doing a sysout each step of the way.<br> * You can expect to see the difference between the index number of the last element vs the number of elements in the array. */ public class Arrays_IndexVsSize {       /** * @param args * Not used */ public static void main(String[] args) { //sysoutArrayConstruction(0);//Try to predict the output for this call before uncommenting it sysoutArrayConstruction(1); sysoutArrayConstruction(5); sysoutArrayConstruction(10); sysoutArrayConstruction(14); }       /** * Display the step by step creation and initialization of an array with numberOfElements elements * * @param numberOfElements * The number of elements for the array to be created with */ public static void sysoutArrayConstruction(int numberOfElements) { int[] testArray = new int[numberOfElements]; System.out.println("numberOfElements = " + numberOfElements + " and testArray.length = " + testArray.length); for(int i = 0; i < testArray.length; i++) { testArray[i] = i; System.out.print("index# " + i + " has value: " + i + " || "); } System.out.println(); }   }```

Quote:

for (int i=0; i<len-1; i++){
Where len is anyArray.length is not what it seems. This loop will omit the last index because of the -1 because of the way the conditional is written:
i < len-1
When i = len, the loop will not run. Add the -1 in my sample above and see what happens. So the line in my sample above will become:
for(int i = 0; i < testArray.length-1; i++) {
Run it again. Compare the results.
Now change the line to:
for(int i = 0; i <= testArray.length-1; i++) {
Using <=, and -1 you get the same results as using < and no -1
• November 7th, 2012, 06:01 PM
Actinistia
Re: Using i-1 in programs
@jps You're absolutely correct. I apologize for the misleading information. The message I wanted to get through was horribly worded and I probably should of just posted the link. Again, sorry for the false info. The last thing I wanted to do was confuse the OP. Thank you for the correction and for the sample program.