Click to See Complete Forum and Search --> : I'm not familiar with these java syntax... Could someone please elaborate?
debbie_82
Nov 23rd, 2004, 03:18 AM
private ArrayList<DemoModule> demosList = new ArrayList<DemoModule> ();
for (SwingSet2 ss : swingSets) {
ss.updateThisSwingSet();
}
I haven't seen these in my old books. :(
And well right now they are causing some errors on my eclipse editor... But these snippets are from the demo projects shipped with java 1.5 so I think they should be working right?
How do I make the errors go away?
And what's the purpose of these constructs?
CornedBee
Nov 23rd, 2004, 05:02 AM
I haven't seen these in my old books.
That's because the old books don't mention new features. Those things are new in Java5.
private ArrayList<DemoModule> demosList = new ArrayList<DemoModule> ();
This is called generics. ArrayList is a generic class. This particular version of ArrayList can only store DemoModule and subclass instances. This increases typesafety and reduces keystrokes (because you don't have to cast on retrieval).
for (SwingSet2 ss : swingSets) {
The advanced for-loop or foreach-loop. This code is effectively equivalent to, but much shorter than, this:
for(Iterator it = swingSets.iterator(); it.hasNext(); ) {
SwingSet2 ss = (SwingSet2)it.next();
How do I make the errors go away?
Download the newest version of Eclipse and hope that it supports Java5.
debbie_82
Nov 23rd, 2004, 07:51 PM
I see... Thanks a lot. I guess I have to check out the other features of java1.5 as well. :D
Dillinger4
Nov 23rd, 2004, 11:02 PM
Very weird syntax. :sick: Haven't looked at the new features of 1.5 yet but i think this scjp exam book ive been reading should talk about them soon. I hope. :rolleyes: About the for loop. Where is the actual initialization;loopcondition?
for (SwingSet2 ss : swingSets) {
debbie_82
Nov 23rd, 2004, 11:33 PM
Yeah... it needs some kind of getting used to.
CornedBee
Nov 24th, 2004, 02:24 AM
About the for loop. Where is the actual initialization;loopcondition?
Nowhere. It still uses the keyword 'for' (because the engineers didn't want to introduce yet another keyword - they already introduced 'enum', which already creates huge incompatibility problems), but it's really a 'foreach' loop. Look at PHP's foreach loop, it works exactly like that, except that the arguments are reversed. Or look at C#'s foreach loop, that works exactly the same way, with the keyword 'in' instead of the colon.
vbforums.com
Copyright Internet.com Inc., All Rights Reserved.