Java
  Home arrow Java arrow Page 4 - Java Comes of Age
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
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: starstarstarstarstar / 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:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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

    - Exception Handling Techniques in Java
    - More About Multithreading in Java
    - The Basics of Multiple Threads in Java
    - Data Access Using Spring Framework JDBC
    - 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...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 2 Hosted by Hostway
    Stay green...Green IT