SJSU/Udacity CS046

Lesson 7.2 - Array Lists and Arrays

  1. Arrays
  2. Array Elements
  3. Quiz: First Five Primes
  4. Quiz: Modifying the Array
  5. Quiz: Put 10 in the First and Last Spaces
  6. Quiz: Array of Strings
  7. Quiz: Enhanced For Loop
  8. Arrays or ArrayLists
  9. Partially Filled Arrays
  10. Homework Scores
  11. Homework Scores Continued
  12. Quiz: Read Scores
  13. Quiz: Sum Scores
  14. Quiz: Average Scores
  15. Inserting and Removing Arrays
  16. Quiz: Inserting and Removing Arrays Continued
  17. Quiz: Removing Lowest Score
  18. Quiz: Find Lowest Scoring Position
  19. Can We Streamline More

Quiz: Enhanced For Loop

Which of these loops can be easily rewritten as an enhanced for loop?

  1. Yes|No
    for (i = 0; i < values.length; i++)
    {
       if (values[i] == 0)
       {
          count++;
       }
    }
    
  2. Yes|No
    int sum = 0;
    for (i = 1; i < values.length; i++)
    {
       if (values[i - 1] > values[i])
       {
          sum = sum + values[i]
       }
    }
    
    It possible to write this as an enhanced for loop (try it—you need another variable to remember the previous value), but Sara says it isn't easy. If it's easy for you, great!
  3. Yes|No
    for (i = 0; i < values.length; i++)
    {
       values[i] = i * i;
    }
    
    You can not modify array values in an enhanced for loop.

Problem on this page?

Your name:

Your email address:

Problem description:

To protect against spam robots, please answer this simple math problem:
× =