function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
raushan kumar 34raushan kumar 34 

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

getting error: 
Challenge Not yet complete... here's what's wrong: 
Executing the 'generateStringArray' method failed. Either the method does not exist, is not static, or does not return the proper number of strings.

public class StringArrayTest {
    public static string[] generateStringArray(integer n){
        List<string> l1=new LIst<String>(n);
        for(integer j=0; j< n; j++){
            l1.add(j, 'Test '+j);
        return (l1);
Amit Chaudhary 8Amit Chaudhary 8
Please check below post for same issue
Please find below a little fix for your issue.

The problem was the space between Test and the number you had : Test0, Test1.... and what was necessary was Test 0, Test 1, ... 
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;
And go :
Your Name>Developer Console>Debug>Open Execute Anonymous Window 
Paste below code in it
list<string> myArray = StringArrayTest.generateStringArray(10);

Please let us know if this will help you

maurya sekhar 4maurya sekhar 4
public class StringArrayTest {
    public static List<String> generateStringArray(Integer n ){
        // creating a new empty list
        List<String> testStrings = new List<String>();
        //create a for loop to add strings to the list taking size from the parameter
        for(Integer i  = 0 ; i < n ; i++ ){
            System.debug('i= '+ i);
            testStrings.add('Test' + i);
        return testStrings;


Ctrl + E opens Anonymous Window 

// the above mentioned issue is because of  space between test and 0 use ('Test ' + 0)
This is working!

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

Execute Anonymous Window 

list<String> listvariable = new list<string>();
listvariable = StringArrayTest.generateStringArray(5);
system.debug('**********' +listvariable);