I want to return a list of strings when I call the method generateArrayList(2);
But it shows:
Method does not exist or incorrect signature: void generateArrayList(Integer) from the type anon
Here is my class:
public class StringArrayTest {
public static List<String> generateArrayList(integer n){
List<String> stringArray = new List<String>();
for (integer i=0;i<n;i++){
String str= 'Test'+String.valueOf(i);
stringArray.add(str);
}
return stringArray;
}
}
You have few compile time errors which needs to be corrected before it can work correctly.
integer
: It will either be an int
or an Integer
. Use ArrayList
new List<String>
: It's an interface. You can't instantiate an interface. 'Test'
: Single quotes is used for Character literal, for Strings use double quotes.string.valueOf(i)
. Below, I have corrected all these errors. It should work now.
public static List<String> generateArrayList(int n){
List<String> stringArray = new ArrayList<>();
for (int i=0;i<n;i++){
String str= "Test" + i;
stringArray.add(str);
}
return stringArray;
}