7

I'm writing a simple Apex class as part of the new Salesforce Trailhead developer course. Here's what I'm trying to do:

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.

Here is my code:

public class StringArrayTest {

    //Public Method
    public static void generateStringArray(Integer length) {

        //Instantiate the list
        String[] myArray = new List<String>(length);

        //Iterate throught the list
        for(Integer i=0;i<myArray.size();i++) {

            //Populate the array
            myArray.add(myArray[i]);

            // Write value to the debug log
            System.debug(myArray[i]);

        } //end loop

    }//end method       
 }// end class

I'm sure it's something very simple I'm doing wrong. When I execute right now it's returning:

expecting a semi-colon, found ''

  • when you write a method that must return something(in your case a list of strings), you do not make it void. Change it to String[] or List<String> – Novarg Nov 12 '14 at 14:50
10

You can't instantiate a List like that using an integer. You don't need to explicitly specify how much items are going into the list when it is created. Instead, just remove the integer:

//Instantiate the list
String[] myArray = new List<String>();

You're logic in your loop seems to be incorrect as well. Specifically, myArray's size will always be 0 when you instantiate it. Change your loop logic to:

for(Integer i=0;i<length;i++) {
   // code
}

You want to generate a List of strings based on the i variable. You will want to change your internal logic to:

//Populate the array
myArray.add('Test ' + i);

// Write value to the debug log
System.debug(myArray[i]);

Finally, you want to return that Array. You need to change your method so the return type is no longer void by changing its signature:

public static String[] generateStringArray(Integer length)

then it is just a matter of returning that array at the end of your method:

return myArray;

This would bring it all together as:

public class StringArrayTest {
    //Public Method
    public static String[] generateStringArray(Integer length) {
        //Instantiate the list
        String[] myArray = new List<String>();

        //Iterate throught the list
        for(Integer i=0;i<length;i++) {
            //Populate the array
            myArray.add('Test ' + i);

            // Write value to the debug log
            System.debug(myArray[i]);
        } //end loop

        return myArray;
    }//end method       
}// end class

Some more info on Arrays and Lists, Loops, and Class methods for future reference.

  • 2
    Right. But his loop would then end immediately as there's not much to loop over. I think the core of the class needs some basic rethinking – FrankZ Nov 12 '14 at 14:13
  • Yep, good call @FrankZ. I realized that after I posted it. I solved the first problem, but that immediately led to another. – Jesse Altman Nov 12 '14 at 14:22
  • 1
    Just like I forgot to return a List of Strings - and not just me :-) – FrankZ Nov 12 '14 at 14:34
  • I just stumbled across this on the active list, and I noticed that this answer is slightly incorrect. You can specify the number of initial elements, which has a very minor performance benefit. The correct syntax uses the square brackets (new String[length];). This reduces the number of heap allocations that occur, which can improve performance. – sfdcfox Feb 15 '17 at 23:42
4

Problem is on the following line

String[] myArray = new List<String>(length);

Lists in Apex do not have constructor for creating lists of predefined length (see doc). This should solve the problem:

String[] myArray = new List<String>();

P.S. to satisfy your requirements, you will also have to change for-loop condition like this:

for(Integer i=0;i<length;i++) {
  • They sort of do have a constructor for creating lists of a predefined length. You go: String[] myArray = new String[length]; – Caspar Harmer Feb 16 '17 at 1:14
4

Here's my shot at it:

public class StringArrayTest {

    //Public Method
    public static List<String> generateStringArray(Integer length) {

        //Instantiate the list
        List<String> myArray = new List<String>();

        //Iterate throught the list
        for(Integer i=0;i<length;i++) {

            //Populate the array
            myArray.add('Test ' + i);

            // Write value to the debug log
            System.debug(myArray[i]);

        } //end loop

        return myArray;

    }//end method       
 }// end class
  • 1
    You are missing The 'generateStringArray' method must return an array (or list) of strings – BarCotter Nov 12 '14 at 14:24
  • 1
    @FrankZ you need to change it from a void method – Jesse Altman Nov 12 '14 at 14:36
  • @JesseAltman true. I fixed it and tested the class now. – FrankZ Nov 12 '14 at 14:51
1

One thing missed from the above answers . ..

Arrays are zero based you need to subtract one from the input variable or start the loop from 1.

Unless that is taken into account when passing in the data.

If the input (length) is 4

 public static List<String> generateStringArray(Integer length) 

..

   for(Integer i=0;i<length;i++) {

        //Populate the array
        myArray.add('Test ' + i);

        // Write value to the debug log
        System.debug(myArray[i]);
    } 

The array size will be 5. so the output will be 0, 1, 2, 3, 4

   for(Integer i=0;i<length -1 ;i++) {

        //Populate the array
        myArray.add('Test ' + i);

        // Write value to the debug log
        System.debug(myArray[i]);
    } 

array size is 4, and the output 0,1,2,3

1

You can specify the number of initial elements, which can provide a speed benefit for large arrays. Here's the modified version:

public class StringArrayTest {
    public static String[] generateStringArray(Integer length) {
        String[] results = new String[length];
        for(Integer i = 0; i < length; i++) {
            results[i] = 'Test '+i;
        }
        return results;
    }
}
  • That's what I was thinking too... – Caspar Harmer Feb 16 '17 at 1:12

protected by Community Feb 15 '17 at 23:35

Thank you for your interest in this question. Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).

Would you like to answer one of these unanswered questions instead?

Not the answer you're looking for? Browse other questions tagged or ask your own question.