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

Thread: For enhanced loop

  1. #1
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default For enhanced loop

    Do the enhanced loop work with double? I get a loss of precision in the for loop at "double count". What can I change to make it work or do I just have to use for loop instead of enhanced? Thanks

            double []x1 = new double[21];
            double increment = 0.1;
            double start = 1.00;
            for(double count:x1)
            {
                x1[count] = (start - increment);
                start = x1[count];
            }


  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,436
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: For enhanced loop

    Why are you using a double for an index to an array? That makes no sense.

    --- Update ---

    string can be used for an index to an array
    Can you post an example of that? I've always used only ints for array indexes.

    Do the enhanced loop work with double?
    Yes an enhanced loop will work with double (or any data type)
    The problem with the posted code is the array indexing.
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: For enhanced loop

    I believes this is using the string as indexes right? Or I might be wrong.
    String[] names = { "fred", "jane", "eric" };
     
    for ( String name : names )
    {
      System.out.println( name );
    }

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,436
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: For enhanced loop

    You said:
    string can be used for an index to an array
    Can you post an example where a String is used as an index to an array? The code in post #3 does not use a String as an index. The code would look like this:
    anArray["a String"]
    If you don't understand my answer, don't ignore it, ask a question.

  5. #5
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: For enhanced loop

    Oh, I'm sorry. I though indexes of the array was referring to what is used to define the index of the array inside the for loop. For indexes to an array, they can only be int right?

  6. #6
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,436
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: For enhanced loop

    Yes
    If you don't understand my answer, don't ignore it, ask a question.

  7. #7
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: For enhanced loop

    Could you give me a hint on how to make the double work with for enhanced loop? Do I have to declare an "int" inside the loop and used that as the indexes or perhaps casting? I try both but none give me a double back but an int instead.

  8. #8
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,436
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: For enhanced loop

    how to make the double work with for enhanced loop?
    The enhanced loop in post#1 works.
    The problem is using a double to index an array. Array indexes should be int. If you must use a double you need to convert it to an int. What int value should 2.9 be converted to? 2 or 3
    If you don't understand my answer, don't ignore it, ask a question.

  9. #9
    Member
    Join Date
    Dec 2012
    Posts
    127
    My Mood
    Angelic
    Thanks
    19
    Thanked 0 Times in 0 Posts

    Default Re: For enhanced loop

    2.9 to int would be "2". So if I wanted to used int for the double array, do I need to declare a new int inside the loop that can be used to index the array ? Thanks

  10. #10
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    SW Missouri
    Posts
    20,436
    Thanks
    49
    Thanked 2,216 Times in 2,189 Posts

    Default Re: For enhanced loop

    If 2.9 is to be used as 2, you could cast the double to an int.
    If you don't understand my answer, don't ignore it, ask a question.

  11. The Following User Says Thank You to Norm For This Useful Post:

    maple1100 (January 2nd, 2013)

Similar Threads

  1. For loop, the first command in the loop does not get executed the 2nd time..
    By lina_inverse in forum Loops & Control Statements
    Replies: 1
    Last Post: October 16th, 2012, 09:00 PM
  2. [SOLVED] Please help with my while loop that turned into infinite loop!
    By Hazmat210 in forum What's Wrong With My Code?
    Replies: 9
    Last Post: March 10th, 2012, 10:22 PM
  3. Converting a while loop to a for loop and a for loop to a while loop.
    By awesom in forum Loops & Control Statements
    Replies: 3
    Last Post: February 26th, 2012, 07:57 PM
  4. Syntax for 3D array enhanced
    By mwr76 in forum What's Wrong With My Code?
    Replies: 4
    Last Post: September 22nd, 2011, 07:02 PM
  5. [SOLVED] My while loop has run into an infinite loop...?
    By kari4848 in forum Loops & Control Statements
    Replies: 3
    Last Post: March 1st, 2011, 11:05 AM