Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2021 - online Python 3 training - see ((here)).

Our plans were to retire in summer 2020 and see the world, but Coronavirus has lead us into a lot of lockdown programming in Python 3 and PHP 7.
We can now offer tailored online training - small groups, real tutors - works really well for groups of 4 to 14 delegates. Anywhere in the world; course language English.

Please ask about private 'maintenance' training for Python 2, Tcl, Perl, PHP, Lua, etc.
for and foreach in Java

Java has had a "traditional" for loop from the beginning:
  for (int k=0; k<Allwords.size(); k++) {

That's three semicolon separated elements:
• initial setting
• test to see if loop should continue
• action to be taken before each re-test

So that when that's used in a loop such as this:
  for (int k=0; k     System.out.print(Allwords.get(k) );
    System.out.println(" " +k);
    }

you can handle each member of a collection (array, ArrayList, Vector, etc) in turn. But - if you don't actually need the position number (that's also known as the index - k in this example) - that's a considerable amount more syntax than you need.

From Java 1.5 (that's also know as Java 5), an alternative form of the for loop has been available which is synonymous with the foreach loop in languages such as PHP. Here's an example:
  for (Object beads: Allwords) {
    System.out.println(beads);
    }


In effect, that's a shorthand notation. There's an iterator that passes the reference for each object in the collection in turn into the beads variable, so that you can process each directly without the need to go via the variable k.

So the for-with-iterator is a great mechanism form processing each member of a collection where you're not concerned with the position number, nor the concept of "next" or "previous" records - but if position number is important, you'll want to stick with the traditional form or the loop.




(written 2010-04-22, updated 2010-04-23)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
J714 - Java - Fundamental classes
  [42] Do languages change? - (2004-09-08)
  [1062] Java sorting - comparable v comparator - (2007-02-02)
  [1502] Java, sorting, ArrayList example, generics - (2008-01-11)
  [1910] Java - Generics - (2008-11-27)
  [2323] Java Collection Objects in the java.util package - (2009-08-05)
  [2418] Viv.java uses unchecked or unsafe operations - explanation and cure - (2009-09-24)
  [2421] Sorting Collections of Objects in Java - (2009-09-25)
  [2649] Length, size or capacity in Java? - (2010-02-24)
  [2920] Sorting - naturally, or into a different order - (2010-08-14)
  [3048] String handling - from first steps to practical examples - (2010-11-13)
  [4330] Java - factory method, encapsulation, hashmap example - (2014-11-27)
  [4396] Java Utility class - flexible replacement for array. Also cacheing in objects and multiple catch clauses example. - (2015-01-16)
  [4421] How healthy are the stars of stage and screen? - (2015-02-09)
  [4431] A Java servlet that is also a stand alone program. And a server that is also a web client. - (2015-02-19)

J704 - Java - Loops and Conditional Statements
  [353] Wimbledon Neck - (2005-06-20)
  [1220] for loop - how it works (Perl, PHP, Java, C, etc) - (2007-06-06)
  [1582] Ruby, C, Java and more - getting out of loops - (2008-03-19)
  [1696] Saying NOT in Perl, PHP, Python, Lua ... - (2008-07-04)
  [3038] Setting up individual variables, and arrays, in Java - some commented examples - (2010-11-09)
  [3200] How a for loop works Java, Perl and other languages - (2011-03-12)
  [3397] Does a for loop evaluate its end condition once, or on every iteration? - (2011-08-18)
  [4322] Learning to Program - the conditional statement (if) - (2014-11-21)
  [4323] Learning to program - Loop statements such as while - (2014-11-22)
  [4346] A behaviour driven example of writing a Java program - (2014-12-09)
  [4427] Java example - for loop and conditionals from course exercise - (2015-02-16)


Back to
Travel Troubles and Jesus again
Previous and next
or
Horse's mouth home
Forward to
Type checking, Java arrays and collections
Some other Articles
What is all this SESSION stuff about? (PHP)
Improving your function calls (APIs) - General and PHP
Perl Course FAQ
Type checking, Java arrays and collections
for and foreach in Java
Travel Troubles and Jesus again
Asking about Jesus
Training Courses - time of change
Bird Watching on the Melksham Avon
Uploading a document or image to its own URL via a browser
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2021: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2734_for ... -Java.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb