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
KMK91KMK91 

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.

William TranWilliam Tran
Madhu,

it looks like you are doing trailhead, be sure to read the example from top to bottom before trying out the exercise.

That said, here's the general guideline.

The Apex class must be called 'StringArrayTest' and be in the public scope.
--> public class StringArrayTest{
The Apex class must have a public static method called 'generateStringArray'.
-->public static List<String> generateStringArray(Integer n) {

The 'generateStringArray' method must return an array (or list) of strings.
-->public static List<String> generateStringArray(Integer n) {

Each string must have a value in the format 'Test n' where n is the index of the current string in the array.
   s= 'Test ' + x;

The number of returned strings is specified by the integer parameter to the 'generateStringArray' method.
  public static List<String> generateStringArray(Integer n) {


The whole solution:
 
public class StringArrayTest{

    public static List<String> generateStringArray(Integer n) {
        List<String> ListFields = new List<String>();
        String s;
        integer x;
        for (x=0;x<n;x++){
            s= 'Test ' + x;
            ListFields.add(s);}  
            return(ListFields);
    }
}

 
KMK91KMK91
I'm getting error like method doesn't exit..........
 
KMK91KMK91
Thank you william for given best answer to me
 
KMK91KMK91
can you tell me how to execute this code?
 
KMK91KMK91
this is the code i'm new to the salesforce
public class StringArrayTest{

    public static List<String> generateStringArray(Integer n) {
        List<String> ListFields = new List<String>();
        String s;
        integer x;
        for (x=0;x<n;x++){
            s= 'Test ' + x;
            ListFields.add(s);}  
            return(ListFields);
            
    }
}
William TranWilliam Tran

To run it use:

StringArrayTest.generateStringArray(10);

where 10 can be any number.

In the developer's console type this in:

System.debug(StringArrayTest.generateStringArray(10));

and this should be the result:

USER_DEBUG [1]|DEBUG|(Test 0, Test 1, Test 2, Test 3, Test 4, Test 5, Test 6, Test 7, Test 8, Test 9)

Thx
Amit Chaudhary 8Amit Chaudhary 8
Hi Madhukar k 16,

Your code should be like below.
public class StringArrayTest {
        public static String[] generateStringArray(Integer length) {
        String[] myArray = new List<String>();
        for(Integer i=0;i<length;i++) {
           myArray.add('Test ' + i);

            System.debug(myArray[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);
system.debug('************'+myArray);
Please let us know if this will help you

Thanks
Amit Chaudhary



 
KMK91KMK91
Thank you william and Amit