Java
  Home arrow Java arrow Page 4 - Java Comes of Age
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Mobile Linux 
App Generation ROI 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

Java Comes of Age
By: Simon White
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 20
    2004-04-07

    Table of Contents:
  • Java Comes of Age
  • Type-Safe Enumerations
  • Static Import and Generics
  • Enhanced For Loop
  • Auto-boxing, Auto-Unboxing, and VarArgs
  • Meta-data

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Java Comes of Age - Enhanced For Loop


    (Page 4 of 6 )

    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 Articles
    More By Simon White


     

       

    JAVA ARTICLES

    - New Object Initialization in Java
    - Adding Images With iTextSharp
    - Adding Columns With iTextSharp
    - Creating Simple PDF Files With iTextSharp
    - The Spring Framework: Understanding IoC
    - Introducing the Spring Framework
    - Java Classes
    - Completing the Syntactic Comparison of Java ...
    - Syntactic Comparison of Java and C/C++
    - Java Statements
    - Conditionals, Expressions and Other Java Ope...
    - Java Operators
    - Primitive Data Types and Basic Language Rule...
    - Java and Object-Oriented Programming
    - Java Beginning Programming





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 4 hosted by Hostway
    Stay green...Green IT