Results 1 to 6 of 6
  1. #1
    BentByBogus is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Default [SOLVED]Why is this not aquivalent ?

    Hi!

    First off thanks for having this forums. Seems to be a great place to look for help.

    Now on with the problem at hand.
    I am working through "Sam's Teach Yourself Java 6 in 21 Days" at I am currently visiting loops.
    There is a sample application that converts an array of int to an array of float by casting and copying from one array to another with a while loop. To get a better grasp of things I thought rewriting it into a for loop might help. This is not working. Needles to say I am a bloody newbie.
    Here is the code snippet from the book:
    Java Code:
    int j = 0;
    while ( j < array1.length) {
    array2[j] = (float) array1[j];
    System.out.print(array2[j++] + " ");
    }
    This is what I have done:
    Java Code:
    for (int j = 0; j<array1.length; j++){
    	array2[j]=(float)array1[j];
    	System.out.println(array2[j++]+ " ");
    }
    In my creation it only shows every 2nd element of the array2 starting with array2[0].
    When I use System.out.println(array2[1]); for example it returns '0'. Am I missing something?
    Thanks a lot for looking into this.
    Last edited by BentByBogus; 10-08-2010 at 09:44 AM. Reason: solved

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    In the second loop, you are incrementing j twice. Once in the for loop, and once in the array2 reference.

  3. #3
    BentByBogus is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Default

    Awwww that much for getting a grasp on things...
    Well yes now I can clearly see it. And thats what makes the difference between a while and a for loop...;)
    Thanks pal!

  4. #4
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    You're welcome! I noticed you tried to edit the title of the first post to say [SOLVED]; you can actually select the "Mark Thread Solved" option from the tools menu in the upper right of the thread page.

  5. #5
    m00nchile is offline Senior Member
    Join Date
    Feb 2010
    Location
    Ljubljana, Slovenia
    Posts
    470
    Rep Power
    5

    Default

    Quote Originally Posted by BentByBogus View Post
    Java Code:
    int j = 0;
    while ( j < array1.length) {
    array2[j] = (float) array1[j];
    System.out.print(array2[j++] + " ");
    }
    This is what I have done:
    Java Code:
    for (int j = 0; j<array1.length; j++){
    	array2[j]=array1[j];
    	System.out.println(array2[j++]+ " ");
    }
    Hmm, when I saw this code snippet (the one from the book, not yours), I immediately, before even reading your problem jumped at this:
    Java Code:
    System.out.print(array2[j++] + " ");
    Instruction books really should steer clear of shortcuts like these to make the program flow more apparent. You probably wouldn't have had this problem if that line was expanded to say:
    Java Code:
    System.out.print(array2[j] + " ");
    j++;
    Ever seen a dog chase its tail? Now that's an infinite loop.

  6. #6
    BentByBogus is offline Member
    Join Date
    Oct 2010
    Posts
    6
    Rep Power
    0

    Default

    Quote Originally Posted by m00nchile View Post
    System.out.print(array2[j] + " ");
    j++;
    [/CODE]
    Funny that you mention it. Exactly what I have done than on my own to check if results are the same.
    Again thanks guys for helping a poor fellow out here...;)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •