Pages

Tuesday, March 7, 2017

Apex Basics & Database "Get Started with Apex"

Create an Apex class that returns an array (or list) of strings.

Create an Apex class that returns an array (or list) of formatted strings ('Test 0', 'Test 1', ...). The length of the array is determined by an integer parameter.
  • The Apex class must be called 'StringArrayTest' and be in the public scope.
  • The Apex class must have a public static method called 'generateStringArray'.
  • The 'generateStringArray' method must return an array (or list) of strings. Each string must have a value in the format 'Test n' where n is the index of the current string in the array. The number of returned strings is specified by the integer parameter to the 'generateStringArray' method.
Solution:

StringArrayTest Class:

public class StringArrayTest {
    public static List<String> generateStringArray(Integer n){
        List<String> myArray = new List<String>();
        for(integer i = 0; i < n; i++){
            myArray.add('Test '+i);
        }
        return myArray;
    }

}

Note: Please be careful with the space in 'Test '+i when you add into the List.