[Bluej-discuss] Ex 4.51

Davin McCall davmac at bluej.org
Thu Oct 5 00:49:27 BST 2006


To use the enhanced for loop (also called the "for each loop") the "Expression" part must evaluate to a collection which you can iterate through (such as a list). The "Type" should be the type contained in the list. The integer counter is no longer needed - the variable ("Identifier") will be given the value of each of the items contained in the list in turn.

In this case the error saying that index is not a variable is because you have the Expression part containing a reference to the Identifier; however, in this type of for loop, the Identifier is not visible (available for use) in the Expression part.

Davin


On Wed, 4 Oct 2006 13:51:09 -0400 (EDT)
John Simonds <simonj2 at sage.edu> wrote:

> But this exercise called for a for each loop (enhanced for loop) - which the book shows as :
> for ( Type Identifier : Expression )
>             Statement
> and does not show semi -colon
>  
>  
>  
> John Simonds
> Systems Technologist
> simonj2 at sage.edu
> (518)244-3111
> (518)292-1734
>  
>  
>  
> 
> 
> ---------------------------------------  
> Original E-mail  
> From: Brian Brookwell  
> Sent: 10/04/2006 01:41 PM  
> To: John Simonds , General discussion for users of BlueJ  
> Subject: Re: [Bluej-discuss] Ex 4.51  
> 
> 
> On 4-Oct-06, at 11:38 AM, John Simonds wrote:  
> 
> >  
> > I have the following code for the for each in exercise 4.51:  
> >  
> > public void listNotes( )  
> >  
> > {  
> >  
> > index = 0;  
> >  
> > for(int index : index ++) {  
> > ^ should be a semi-colon and for loop  
> > has three components eparated by ; not two  
> >  
> > System.out.println(notes.get(index));  
> >  
> > }  
> > }  
> >  
> > This results in an error. I am not understanding why it doesn't  
> > see index as a variable.  
> > John Simonds  
> > Systems Technologist  
> > simonj2 at sage.edu  
> > (518)244-3111  
> > (518)292-1734  
> >  
> >  
> > _______________________________________________  
> > mailing list bluej-discuss at bluej.org  
> > To unsubscribe or change your preferences, go to  
> > http://lists.bluej.org/mailman/listinfo/bluej-discuss  
> 
> 
> 
>  


More information about the bluej-discuss mailing list