Home arrow Java & J2EE arrow Page 4 - Java Comes of Age

Enhanced For Loop - Java

Time to open the Champagne -- Java 1.5 is out, and the language has finally come of age! With the new Java 1.5 specification, Java now contains features that make it feel like a proper "grown-up" language. The rest of this article will introduce you to these new features. To try out the features for yourself, simply download Java 1.5 from Sun’s website and give it a whirl. Note that you’ll need to compile the code using the –source 1.5 option; otherwise, you’ll get compilation errors when using the new features.

TABLE OF CONTENTS:
  1. Java Comes of Age
  2. Type-Safe Enumerations
  3. Static Import and Generics
  4. Enhanced For Loop
  5. Auto-boxing, Auto-Unboxing, and VarArgs
  6. Meta-data
By: Simon White
Rating: starstarstarstarstar / 20
April 07, 2004

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

It is common to iterate through Collections, yet the Java idiom for doing so has, until now, been a little cumbersome. We would have to declare an Iterator, explicitly check whether another object is available (as a loop condition), and then retrieve the object if there is one available. And there's that tricky cast to contend with too.

Java 1.5 has simplified the idiom, so that, for example, instead of writing:


private void printCards(Collection deck) {
        Iterator iter 
deck.iterator();
        
while (iter.hasNext()) {
            Card card 
= (Carditer.next();
            System
.out.println(card.toString());
        
}
    
}

you can now write:


private void printCards(Collection<Carddeck) {
        
for (Card card deck) {
            System
.out.println(card.toString());
        
}
    


This saves a bit of typing but, more importantly, is less error-prone. I certainly welcome the omission of the cast.

You can also use the enhanced for-loop for iterating through arrays. I omitted to mention in the earlier section on enumerations that the method values() can be applied to an enumeration to retrieve an array of all the possible values. So using the new for-loop construct you can create a deck of cards with:


List<Carddeck = new ArrayList<Card>();
 
for (
CardSuit suit CardSuit.values()) {
    
for (CardValue val CardValue.values()) {
        Card card 
= new Card(valsuit);
        deck
.add(card);
    
}
}



 
 
>>> More Java & J2EE Articles          >>> More By Simon White
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

JAVA & J2EE ARTICLES

- More Java Bugs Lead to More Attacks
- Oracle's Java One Brings News, Surprises
- Oracle Patches Java Runtime Environment
- Apple Syncs Java Update with Oracle
- Spring 3.1 Java Development Framework Compat...
- Jelastic Java PaaS Availability and Pricing ...
- NetBeans 7.1 Released, Supports JavaFX 2
- SolarWinds Releases Newest Version of Java M...
- Free Monitoring Tool for Java Apps on Heroku
- Heroku Adds JCloud Platform Support, Java 7 ...
- Java SE 8 Speculation in Full Swing
- Java SE 7 Now Available
- New JVM Language and Java Reporting Tool
- Java 7 Release Update and New Eclipse Toolkit
- The Best Java Netbeans IDE Plugins

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: