-
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
-
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
-
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 .
-
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.
-
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.