-4

I am working on creating an array of strings from a list of strings. So far, I have the following code:

ArrayList<String> layerChoices = new ArrayList<>();
for(IFeatureLayer layer : layerList){
   layerChoices.add(layer.getName());
}
String[] choices = (String[])layerChoices.toArray();

The issue being that toArray() returns an Object[] and not a String[] which is producing a class cast exception when trying to cast to a String[]. Is there a simple way to accomplish my goals other than a for loop in which I would iterate through the Object[] , cast each Object to a String, and then add each String to a String[]? Seems like a lot of work for a simple task...

GregH
  • 5,125
  • 8
  • 55
  • 109

2 Answers2

3

Use it like this - String [] myArray = myList.toArray(new String[myList.size()]);

Raman Shrivastava
  • 2,923
  • 15
  • 26
2

Try:

String[] choices = layerChoices.toArray(new String[layerChoices.size()]);
Jean Logeart
  • 52,687
  • 11
  • 83
  • 118