CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5

Thread: for loop

  1. #1
    Join Date
    Oct 2004
    Posts
    77

    for loop

    i am reading core java 2 vol 1 by horstmann & cornell .in this book ,in the chapter of event handling .he has written method

    Code:
    public void paintComponent(Graphics g)
    {
    	super.paintComponent(g);
    	Graphics2D g2=(Graphics2D) g;
    	
    	for(Line2D l: lines)
    	{
    		g2.draw(l);
    	}
    		
    }
    
    private ArrayList<Line2D> lines;
    i have not understood what does this 'for' loop mean.it is giving me error too.
    how to fix it

    also in the declaration
    private ArrayList<Line2D> lines;

    <Line2D> gives me error

  2. #2
    Join Date
    Apr 2005
    Location
    Norway
    Posts
    3,934

    Re: for loop

    With J2SE 5.0 Sun has introduced a couple of new features, one of them is the 'for-each' loop.

    Instead of doing this :
    Code:
    for (int i = 0; i < lines.size(); i++)
    {
        Line2D l = lines.get(i);
        g2.draw(l);
    }
    you can do this:
    Code:
    for(Line2D l: lines)
    {
        g2.draw(l);
    }
    Another feature is support for 'generics'.
    Code:
    private ArrayList<Line2D> lines;
    This tells the compiler that 'lines' is an ArrayList meant for objects of type 'Line2D', thus eliminating the need for casting:
    Code:
    // in J2SE 1.4 you would do this
    Line2D l = (Line2D) lines.get(i);
    
    // but in J2SE 5.0 you can do this as well
    Line2D l = lines.get(i);
    For more check out:
    http://java.sun.com/j2se/1.5.0/docs/...e/foreach.html
    and
    http://java.sun.com/j2se/1.5.0/docs/.../generics.html

    Again, these are Java J2SE5.0 (or 1.5.0, or whatever) features, so you propbably need a newer SDK.

    - petter

  3. #3
    Join Date
    Oct 2004
    Posts
    77

    Re: for loop

    THanks for the detailed explaination....i am very grateful to u!

    i have installed jdk1.5.0_02 and i am using eclipse as IDE but its giving me eror on these two things .

  4. #4
    dlorde is offline Elite Member Power Poster
    Join Date
    Aug 1999
    Location
    UK
    Posts
    10,163

    Re: for loop

    You may have to tell Eclipse to allow the new Java 5.0 syntax...

    At the source of every error which is blamed on the computer, you will find at least two human errors, one of which is the error of blaming it on the computer...
    Anon.
    Please use &#91;CODE]...your code here...&#91;/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.

  5. #5
    Join Date
    Sep 2004
    Posts
    247

    Re: for loop

    Eclipse 3.0 doesn't support Java 5.0 features. I think Eclipse 3.1 M4 and later support Java 5 but there are not production ready yet.

Posting Permissions

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





Click Here to Expand Forum to Full Width

Featured