Wednesday 29 August 2012

Arrays and Lists - Apex Language Fundamentals


Arrays and Lists:
Apex has a list collection type that holds an ordered collection of objects. List elements can be accessed with an index or can be sorted if they’re primitive types, such as Integers or Strings. You’ll typically use a list whenever you want to store a set of values that can be accessed with an index. As you’ll see in later tutorials, lists are also used to hold the results of queries.
You can access an element of a list using a zero-based index, or by iterating over the list.  Here's how to create a new list, and display its size:
List<Integer> myList = new List<Integer>();
System.debug(myList.size());
Arrays in Apex are synonymous with lists—Apex provides an array-like syntax for accessing lists. Here is an alternative way to create exactly the same list:
Integer[] myList = new List<Integer>();
You can also define a list variable and initialize it at the same time as shown in the following example, which displays the string 'two':
List<String> myStrings = new List<String> { 'one', 'two' };
To add a new element to a list, use the add method.
myList.add(101);
You can use the array notation to get or modify an existing value.
// Get the first element 
    
Integer i = myList[0];
// Modify the value of the first element 
    
myList[0] = 100;

Try It Out

This snippet creates a list and adds an integer value to it. It retrieves the first element, which is at index 0, and writes it to the debug output. This example uses both the array notation, by specifying the index between brackets, and the get method to retrieve the first element in the list.
Integer[] myList = new List<Integer>();
//Adds a new element with value 10 to the end of the list 
    
myList.add(10);  

// Retrieve the first element of the list 
    
// using array notation 
    
Integer i = myList[0];  
// or using the get method 
    
Integer j = myList.get(0);
System.debug('First element in the array using myList[0] is ' + i);
System.debug('First element in the array using myList.get(0) is ' + j);
Here is a portion of the output when you run this snippet in the Developer Console:

Debug output for the value of the first element in the list.
This next snippet creates a list and adds an integer value to it. It modifies the value of the first element and writes it to the debug output. Finally, it writes the size of the list to the debug output. This example uses both the array notation, by specifying the index between brackets, and the set method to modify the first element in the list.
Integer[] myList = new List<Integer>{10, 20};

// Modify the value of the first element 
    
// using the array notation 
    
myList[0] = 15; 
// or using the set method 
    
myList.set(0,15);
System.debug ('Updated value:' + myList[0]);

// Return the size of the list 
    
System.debug ('List size: ' + myList.size());
Here is a portion of the output when you run this snippet in the Developer Console:

Debug output for the updated list value and the list size.

1 comment: