Simple Apex Class to return list of string based on the length provided

Sample Code:

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

Cheers!!!

3 comments:

  1. Hi Sebastian,

    I executed "StringArrayTest.generateStringArray(6);" in the Anonymous Window but it didn't return a list of strings...
    I expected to see my listString returned in the "Debug Only" mode.

    Do you have any idea what the issue might be?

    Thanks,

    Weronika

    ReplyDelete
    Replies
    1. public class StringArrayTest {
      //Public method
      Public static List generateStringArray(integer n){
      List testarray = new List();
      for (integer i=0;i<n;i++)
      {
      testarray.add('test'+i);
      system.debug(testarray[i]);
      }
      return testarray;
      }

      }

      Delete
  2. Thanks a lot for this!

    ReplyDelete