How colon : used in for{} loop?


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 3 of 3

Thread: How colon : used in for{} loop?

  1. #1
    Join Date
    Aug 2006
    Posts
    9

    Exclamation How colon : used in for{} loop?

    Taking a Java test I found a for{} loop which had a : character in it?... like this...

    for { : }

    I was supposed to fill in some objects, but I have no idea how a : character is used in a for loop?

    Any comments?
    JON

  2. #2
    Join Date
    Nov 2006
    Location
    NEW DELHI, INDIA
    Posts
    22

    Thumbs up foreach

    Hi!

    Are u sure it is of the format u provided above? or shown as below:

    public class ForeachArray {
    public static void main(String args[]) {
    String[] data = { "Asia", "Japan" };
    for (String s : data) {
    System.out.println(s);
    }
    }
    }
    Thanks,
    abhi

  3. #3
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    The enhanced for loop (in reality, an application of the Iterator) allows you to do something with each object in a collection.

    The syntax then is to tell the compiler that, for each object in the collection, do something ...

    for( String s [THE OBJECT WHICH IS THE CURRENT ELEMENT] : myArrayList [THE COLLECTION])
    {
    replace( s, a);
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center