+ Start a Discussion
kotipalli srikanthkotipalli srikanth 

String array challenge from trailhead.

public class StringArrayTest {
    public Static List<String> generateStringArray(integer n){
/* i want to know that, here the integer n is  not initialized with any value, so how i<n works*/
         List<String> myArray = new List<String> ();
        for(integer i=0; i<n; i++){
            myArray.add('Test' +i);
            System.debug(myArray[i]);
        }
            return myArray;
    }
}
// And also my above program is failing .
//help me please.
Best Answer chosen by kotipalli srikanth
Manj_SFDCManj_SFDC
Your generateStringArray method will be invoked from other class or may be in the same class with the value for n passed as parameter, if not you need to invoke it based on your requirements,please check 

All Answers

Manj_SFDCManj_SFDC
Your generateStringArray method will be invoked from other class or may be in the same class with the value for n passed as parameter, if not you need to invoke it based on your requirements,please check 
This was selected as the best answer
Santhosh NairSanthosh Nair
Below worked!

public class StringArrayTest {
    public static String[] generateStringArray (Integer iCount) {
        String[] myArray = new String[] {};
        system.debug('Input parameter iCount: ' + iCount);
        for (integer i=0; i<iCount; i++) {
            myArray.add('Test ' + i);
            system.debug('Array [' + i + ']: ' + myArray.get(i));
        }
        return myArray;
    }
}

and you can use below code in Execute Anonymous window to test the code.

String[] finalArray = new String[] {};
finalArray = StringArrayTest.generateStringArray(2);
for (integer i=0; i<finalArray.size();i++){
    System.debug('finalArray[' + i + ']: ' + finalArray.get(i));
}