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 5 of 5

Thread: Using i-1 in programs

  1. #1
    Junior Member
    Join Date
    Oct 2012
    Posts
    7
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Cool 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.


  2. #2
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,424
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 540 Posts

    Default Re: Using i-1 in programs

    I don't see an "i-1" in that code at all.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  3. #3
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default 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)

  4. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default 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*/
    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();
    	}
     
    }


    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

  5. #5
    Member
    Join Date
    Mar 2011
    Posts
    66
    My Mood
    Relaxed
    Thanks
    12
    Thanked 4 Times in 4 Posts

    Default 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.

Similar Threads

  1. Graphics in programs.
    By tyb97 in forum AWT / Java Swing
    Replies: 6
    Last Post: October 11th, 2011, 08:35 AM
  2. programs of java
    By chinu in forum Java Servlet
    Replies: 2
    Last Post: July 26th, 2011, 12:08 PM
  3. Student Programs
    By Suzanne42 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: April 26th, 2011, 09:20 AM
  4. need help make a programs...
    By grendar in forum Loops & Control Statements
    Replies: 3
    Last Post: April 13th, 2011, 01:02 PM
  5. What programs shall i make?
    By DarrenReeder in forum Java Theory & Questions
    Replies: 1
    Last Post: December 27th, 2009, 08:31 AM