Skip to content

Instantly share code, notes, and snippets.

@olgaloza
Created February 27, 2018 00:34
Show Gist options
  • Save olgaloza/47a53ef669a841746840b3c9a567d21d to your computer and use it in GitHub Desktop.
Save olgaloza/47a53ef669a841746840b3c9a567d21d to your computer and use it in GitHub Desktop.
WeekThreeHomework - Trailhead
public class StringArrayTest {
/* WeekThreeHomework from Trailhead module "Get Started with Apex":
* Create an Apex class that returns an array (or list) of strings.
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
The number of returned strings is specified by an input parameter of an Integer type
Each string returned must have a value in the format Test n where n is the index of the current string in the array
*/
public static List<String> generateStringArray(Integer n) {
List<String> TestList = new List<String>();
for(Integer i=0;i<n;i++) {
TestList.add('Test ' + i);
System.debug(TestList[i]);
}
/* Another way of doing a loop
Integer i = 0;
while (i < n) {
TestList.add('Test ' + i);
System.debug(TestList[i]);
i++;
}
*/
return TestList;
}
}
@DoronMaz17
Copy link

Hello,

I've always got this message after trying to test it in the "Open Execute Anonymous Window":

"Static method cannot be referenced from a non static context: List
StringArrayTest.generateStringArray(Integer)"

I am testing it:

StringArrayTest str = new StringArrayTest();
str.generateStringArray(5);

Can you help me please

@Hani2808
Copy link

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

//Execute Anonymous code to call the class above

StringArrayTest.generateStringArray(Enter the value of i );

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment