Results 1 to 6 of 6
  1. #1
    Java Riser's Avatar
    Java Riser is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default Enhanced for loop, the other "element"

    **Edit** Ah yes, sorry Fubarable. I was too immersed in this one to remember that it makes no sense unless you've been working on it for hours. Ironically, Fubarable's question to my question half answered it.

    Let me re-formulate the question. In fact, Let me edit the post to show the (now clear) question.

    Java Code:
    vacationDays [0]=10;
    vacationDays [1]=15;
    vacationDays [2]=15;
    vacationDays [3]=15;
    vacationDays [4]=20;
    vacationDays [5]=20;
    vacationDays [6]=25;
    vacationDays [6]=25;
    In that or any array, using the enhanced for loop for (int item:vacationDays);

    System.out.println(Item); displays the values after the"=". So then "item" is the key word for the value after the "=".

    Is there any key word that will display the values inside the "[]"?

    The number inside the brackets would be an important number to be able to display in many programs. In this case, it represents the number of years worked for a company. Is there a way to display the number inside the brackets of an array?
    Last edited by Java Riser; 02-22-2010 at 10:09 AM. Reason: much improved logic

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I don't see anywhere in the code posted where there even exists data representing number of years worked, so I'm not sure how you expect us to answer this question (unless I'm missing something obvious???).
    Last edited by Fubarable; 02-22-2010 at 03:38 AM.

  3. #3
    Java Riser's Avatar
    Java Riser is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

    Default So the better question is:

    Java Code:
    someName [0]=10;
    someName [1]=15;
    someName [2]=20;
    In this or any other array, System.out.println(Item) displays the values after the"=". So therefore "item" is the key word for the value after the "=".

    Is there any key word for the number inside of the brackets? Some way to display the values inside the "[]"?

    The number inside the brackets would be an important number to be able to display with println in some programs. Is there a way to display the number inside the brackets of an array? I want it to also display "1" when it displays "15".

    Hope this makes more sense.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Ah, I get you. That number in the brackets is called the index of the array. So if you need the index, then perhaps you'd be best to avoid the "enhanced" for loop. If you are still required to use an enhanced for loop, then you'll need to create an index variable and increment it in the loop:

    Java Code:
    int index = 0;
    for (int item:vacationDays) {
      System.out.println("vacation days: " + item + ", years in office: " + index);
      index++;  // increment the index
    }
    But this seems kludgy to me, and I'd much prefer to use a standard loop here.

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

    Default

    Yes, using the enhanced for loop makes sense only if the index is not important.

  6. #6
    Java Riser's Avatar
    Java Riser is offline Member
    Join Date
    Feb 2010
    Posts
    12
    Rep Power
    0

Similar Threads

  1. Replies: 1
    Last Post: 01-21-2010, 10:20 AM
  2. Replies: 2
    Last Post: 10-09-2009, 12:28 AM
  3. Replies: 2
    Last Post: 01-24-2009, 07:56 PM
  4. Replies: 1
    Last Post: 10-20-2008, 08:35 AM
  5. Replies: 3
    Last Post: 09-26-2008, 07:05 PM

Tags for this Thread

Posting Permissions

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