RU beehive logo ITEC dept promo banner
ITEC 120
2007fall
ibarland,
jdymacek

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs

lect11b
for-each
looping over a list

review

Declare a list of Doubles; add three numbers to it; retrieve the last number in the list; check whether Math.PI is in the list.

Lists

We can process each element of a list, no matter how long the list is! Java provides a new type of statement, the “for-each loop”:

for ( Double d : dubs ) {  // "for each d in dubs, ..."
  System.out.println( "Currently looking at: " + d.toString() );
  }

Now, we already know that printing to the console is something which we can't build on with other methods, since it doesn't actually return any answer. So we can use loops to actually compute something:

/* Add up elements of a list, with a for-each loop. */
double totalSoFar = 0.0;   // The running total -- an "accumulator" variable.
for ( Double d : dubs ) {  // "for each d in dubs, ..."
  totalSoFar = totalSoFar + d.doubleValue();
  }
// For-each is only appropriate when we want to handle every element exactly the same.
// To-do:
// - write a function average, which computes 
//   the average of a (non-empty!) list of numbers.
// - Augment the above code so you also accumulate the sum-of-squares
//    (or, the absolute values)
// - Augment the above code so you also accumulate a String, with each
//    number separated by a space, with angle-brackets on each side:
//      "< 5.4 -1.2 4.2 0.0 5.4 >"

Lists aren't at all specific to Doubles; they can hold any sort of object at all. Note that technically, the list holds a reference to objects. Let's draw pictures on the board:

// ======================================

Dog d1 = new Dog( "lassie", 15 )
Dog d2 = new Dog( "fido", 3 )
Dog d3 = new Dog( "phydeaux", 5 )

java.util.LinkedList<Dog> pound = new java.util.LinkedList<Dog>();

pound.add( d1 )
pound.add( d2 )
pound.add( d3 )
pound.toString()
pound.contains( d1 )
pound.contains( new Dog( "fido", 3 ) )   /* .equals, or == ?  See docs. */
pound.contains( "fido" )
pound.remove( new Dog( "fido", 3 ) )     /* .equals, or == ?  See docs. */
pound.toString()
pound.remove( d1 )
pound.toString()

a Blah is not a List-of-Blah

A number is not a list of numbers; a Dog is not a LinkedList<Dog>. Think of the list as a bag. The bag might even be empty. It is a common confusion for people to declare a field to be (say) a Treasure, when really what they want to keep track of is a LinkedList<Treasure>. Or, to ask a LinkedList<Dog> to speak(), even though that's a behavior of things in the bag, not a behavior of the bag itself. (Use a for-each loop to work with each object inside the bag.)

a bag of items

Which of the following method calls make sense?

  1. pound.getSound()
  2. d1.get(1)
  3. pound.get(1)
  4. d1.get(1)
  5. Dog d = pound.get(1) followed by d.getSound()
  6. (pound.get(1)).getSound()

Looping over lists of objects

to do:

Things to ask yourself, when writing a loop:

homeinfoexamslectureslabshws
RecipeLawsliessyntaxjava.lang docsjava.util docs


©2007, Ian Barland, Radford University
Last modified 2007.Nov.01 (Thu)
Please mail any suggestions
(incl. typos, broken links)
to iba�rlandrad�ford.edu
Powered by PLT Scheme