ApexSensei

Drills Apex basics

For-each over Accounts

A for-each loop walks every item in a List in order, with no index to manage: for (Account acct : accounts) { ...

Your task

Loop over three Accounts and print each name, then loop safely over an empty List and a null List.

List<Account> accounts = new List<Account>{
    new Account(Name = 'Cedar Labs'),
    new Account(Name = 'Northstar Co'),
    new Account(Name = 'Bluebird Goods')
};
Integer visitedCount = 0;

List<Account> emptyAccounts = new List<Account>();
Integer emptyCount = 0;

List<Account> missingAccounts = null;
Integer nullCount = 0;

Requirements

  1. Visited: Cedar Labs and the other two names should be printed in order
  2. Visited count: 3 should be printed
  3. Empty count: 0 should be printed
  4. Null count: 0 should be printed
  5. for-each loop should be used over accounts
  6. for-each loop should be used over emptyAccounts
  7. missingAccounts != null should be checked
  8. Visited: Cedar Labs should be built at runtime

Sign in to open this drill