for (i = 0; i < numbers.length - 2; i++)
In some codes we write i<numbers.length and in some numbers.length-1 or some others. What does this piece of code actually mean?
Welcome to the Java Programming Forums
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.
>> REGISTER NOW TO START POSTING
Members have full access to the forums. Advertisements are removed for registered users.
for (i = 0; i < numbers.length - 2; i++)
In some codes we write i<numbers.length and in some numbers.length-1 or some others. What does this piece of code actually mean?
Although I am a beginner, this line would mean that the loop you are using will run until the Counter that was created in the statement, or 'i', is two values less than whatever value number has been set equivalent to earlier in your code. It will then repeat this code in the for block until the value of 'i' is two less than the value of number.
Hopefully that helps.
hemla (March 25th, 2013)
Take a look at the tutorial: The for Statement (The Java™ Tutorials > Learning the Java Language > Language Basics)
or ask google. Many modern languages use the same syntax in for statements.
If you don't understand my answer, don't ignore it, ask a question.
Java gives you a lot of power in for loops. I'll give some examples and then try to explain the ones you are asking about.
==Basic For loop==
for(int i=0 ; i<array.length ; i++){
This loop executes once for each element in the array. Since arrays are zero based (indexes go 0,1,2,3 instead of one based 1,2,3,4) we have i start at 0 but we stop if i goes above the length.
You may as "why do we stop before the length, isnt that missing a number?". The answer is no because it is zero based. 0,1,2,3 has 4 elements. So position 3 is the 4th element and if you try to look at position 4 you will get an error.
==How a for loop executes==
for(A;B;C)
A: Executed first thing when you enter the loop. Most commonly used to declare a counter for the loop.
B: Executed at the beginning of each pass. If it results in TRUE then it enters the loop. If it results in FALSE then it skips over the loop and continues with the program.
C: Executed at the end of each pass. Most commonly used to increment the counter by some value.
==Crazy example==
for(String initialString = "ReAlly A sTriNg?" ; initialString.length() > 0 && initialString.contains("?") ; initialString.replaceFirst(String.valueOf(initialString.charAt(initialString.length()/2)), String.valueOf(initialString.charAt(initialString.length()/2)).toUpperCase())){ if(Character.isUpperCase(initialString.charAt(0))){ System.out.println("Upper Case Found"); } else { System.out.println("Lower Case Found"); } initialString = initialString.substring(1); }
==Your question==
I have no idea why someone would subtract 1 or even 2 from the length if they are already using a zero based index. If you could give some context of how you saw it being used i could explain more.
Writing code is your job, helping you fix and understand it is mine.
<-- Be sure to thank and REP (Star icon) those who have helped you. They appreciate it!
hemla (March 25th, 2013)
output at the terminal should look like : noTriples({1, 1, 2, 2, 1}) -> true
noTriples({1, 1, 2, 2, 2, 1}) -> false
Basically there should be no triples in the code. if there are any, it returns false.
public class NoTriples { public static void main(String[] args) { int N=args.length; int[] nums=new int[N]; for(int i=0; i<N;i++){ nums [i]=Integer.parseInt(args[i]); } System.out.println(noTriples(nums)); } public static boolean noTriples(int[] nums) { int i; boolean val = true; for (i = 0; i < nums.length - 2; i++) { if (nums[i] == nums[i+1] && nums[i] == nums[i+2]) { val = false; } } return val; } }
Thats the code if it would help you explain why someone would subtract 1 or even 2 from the length.
if (nums[i] == nums[i+1] && nums[i] == nums[i+2]) {why someone would subtract 1 or even 2 from the length
To keep the above code (in red) from going off the end of the array.
If you don't understand my answer, don't ignore it, ask a question.