The professional, friendly Java community. 21,500 members and growing!

The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.

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.

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.

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:

/** FILE: forumsamples.Arrays_IndexVsSize.java*/packageforumsamples;/**
* @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.
*/publicclass Arrays_IndexVsSize {/**
* @param args
* Not used
*/publicstaticvoid 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
*/publicstaticvoid sysoutArrayConstruction(int numberOfElements){int[] testArray =newint[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();}}

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

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.