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.

Results 1 to 6 of 6

Thread: what does this line mean?

  1. #1
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Question what does this line mean?

    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?


  2. #2
    Junior Member
    Join Date
    Mar 2013
    Posts
    1
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    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.

  3. The Following User Says Thank You to wortz91 For This Useful Post:

    hemla (March 25th, 2013)

  4. #3
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: what does this line mean?

    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.

  5. #4
    Member Chris.Brown.SPE's Avatar
    Join Date
    May 2008
    Location
    Fort Wayne, Indiana
    Posts
    190
    Thanks
    1
    Thanked 31 Times in 31 Posts

    Default Re: what does this line mean?

    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!

  6. The Following User Says Thank You to Chris.Brown.SPE For This Useful Post:

    hemla (March 25th, 2013)

  7. #5
    Member
    Join Date
    Jan 2013
    Posts
    31
    My Mood
    Confused
    Thanks
    12
    Thanked 0 Times in 0 Posts

    Default Re: what does this line mean?

    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.

  8. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,145
    Thanks
    65
    Thanked 2,720 Times in 2,670 Posts

    Default Re: what does this line mean?

    why someone would subtract 1 or even 2 from the length
    if (nums[i] == nums[i+1] && nums[i] == nums[i+2]) {
    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.

Similar Threads

  1. Replies: 10
    Last Post: September 16th, 2011, 07:49 PM
  2. Reading a file line by line using the Scanner class
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  3. Reading a file line by line using the Scanner class
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: April 17th, 2009, 07:34 AM
  4. How to Read a file line by line using BufferedReader?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM
  5. How to Read a file line by line using BufferedReader?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 19th, 2008, 06:32 AM