-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...

0

2 Answers 2

3

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

Sign up to request clarification or add additional context in comments.

Comments

2

Try:

String[] choices = layerChoices.toArray(new String[layerChoices.size()]);

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.