This course is currently a work in progress. Please join the waitlist to receive regular updates.

List in apex

What is a List?

A List in apex is an ordered collection of elements where :

  • Each items is stored at a specific index starting from 0.
  • Lists allow duplicate items.
  • Lists can store simple data types ( like string, integer, etc..).
  • Lists can store complex data types like Sobjects (account, contact) or user defined objects.
0

Blue

1

Red

2

Green

3

Yellow

4

Blue

List example: index on left and value on right

When to use a list

You can use a list when:

  • Order matters, e.g processing items in a specific sequence.
  • Duplicates are allowed, e.g storing multiple instances of the same value.
  • Bulk processing, e.g performing operations on set of records at once (updating a list of accounts).

How to declare a list?

You can declare a integer using the List <datatype> syntax. Following is an example of an empty list of strings

Loading...

In the above example:

  • The keyword List is used to declare a list which can hold string values.
  • The variable name names can be used to operate (add/remove..etc) on the list.
  • Thenames list contain no values.