12

I wish to convert a list of Strings to an array of strings.

I do:

// ccEmails is the list of Strings
String [] emailsAsArray = new String [ccEmails.size()];
Integer i = 0;
for (String singleCCEmail: ccEmails) {
    emailsAsArray[i++] = singleCCEmail;
}

This seems a horrible cumbersome way. Is there a better way?

  • Are you talking Apex? Java? C#? If Apex, there is no such thing as Array. It is all list, even though the syntax looks like array. – pchittum Feb 7 '13 at 15:32
  • sorry...should have noticed. :-) – pchittum Feb 7 '13 at 15:40
25

There is no such thing as array in Apex.

The creators of Apex decided that they would support two different but completely compatible syntax conventions for the same underlying object: List. One looks like array, the other looks like List, but they are both List.

I could literally do this:

List<String> myStrings = new String[2];

myStrings[0] = 'this'; 
myStrings.set(1,'that');
myStrings.add('the other');

System.debug(myStrings);  

This (ugly and unreadable as it is) compiles and runs successfully.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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