Java

TOPIC 23 – ENHANCED FOR LOOP

 

LESSON WORK

 

 

QUESTION 1

Create an ArrayList that hold 20 random numbers.  Use the enhanced for loop to go through all numbers and calculate the total.  Output the total to screen.

 

QUESTION 2

 

a)  Create a Stack object that holds 5 numbers. 

b)  Using an enhanced for loop, output each stack item to the screen (using the pop method).  Notice that this gives you an error.

c)  Replace the above for loop by a traditional for loop.  Careful,  that you loop the correct number of times!