Re: Using i-1 in programs
I don't see an "i-1" in that code at all.
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)
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
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.