Results 1 to 6 of 6
- 02-22-2010, 02:14 AM #1
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.
In that or any array, using the enhanced for loop for (int item:vacationDays);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;
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 09:09 AM. Reason: much improved logic
-
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 02:38 AM.
- 02-23-2010, 02:07 AM #3
So the better question is:
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 "=".Java Code:someName [0]=10; someName [1]=15; someName [2]=20;
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.
-
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:
But this seems kludgy to me, and I'd much prefer to use a standard loop here.Java Code:int index = 0; for (int item:vacationDays) { System.out.println("vacation days: " + item + ", years in office: " + index); index++; // increment the index }
- 02-23-2010, 08:09 AM #5
Senior Member
- Join Date
- Feb 2010
- Location
- Ljubljana, Slovenia
- Posts
- 470
- Rep Power
- 4
Yes, using the enhanced for loop makes sense only if the index is not important.
- 02-23-2010, 11:47 PM #6
Similar Threads
-
How to change my form design from "metal" to "nimbus" in Netbeans 6.7.1?
By mlibot in forum New To JavaReplies: 1Last Post: 01-21-2010, 09:20 AM -
how to override "cancel operation" in "progress bar"
By singswt in forum SWT / JFaceReplies: 2Last Post: 10-08-2009, 11:28 PM -
MoneyOut.println("It took you (whats wrong?>",year,"<WW?) years to repay the loan")
By soc86 in forum New To JavaReplies: 2Last Post: 01-24-2009, 06:56 PM -
the dollar sign "$", prints like any other normal char in java like "a" or "*" ?
By lse123 in forum New To JavaReplies: 1Last Post: 10-20-2008, 07:35 AM -
How do we "Loop" through subfolders to delete files?
By Zrob in forum New To JavaReplies: 3Last Post: 09-26-2008, 06:05 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks