|
-
Nov 23rd, 2004, 04:18 AM
#1
Thread Starter
Banned
I'm not familiar with these java syntax... Could someone please elaborate?
Code:
private ArrayList<DemoModule> demosList = new ArrayList<DemoModule> ();
Code:
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?
Last edited by debbie_82; Nov 24th, 2004 at 12:27 AM.
-
Nov 23rd, 2004, 06:02 AM
#2
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:
Code:
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
-
Nov 23rd, 2004, 08:51 PM
#3
Thread Starter
Banned
I see... Thanks a lot. I guess I have to check out the other features of java1.5 as well.
-
Nov 24th, 2004, 12:02 AM
#4
Dazed Member
Very weird syntax. 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. About the for loop. Where is the actual initialization;loopcondition?
Code:
for (SwingSet2 ss : swingSets) {
-
Nov 24th, 2004, 12:33 AM
#5
Thread Starter
Banned
Yeah... it needs some kind of getting used to.
-
Nov 24th, 2004, 03:24 AM
#6
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.
All the buzzt
 CornedBee
"Writing specifications is like writing a novel. Writing code is like writing poetry."
- Anonymous, published by Raymond Chen
Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|