oa2sa - remember to use standard libs and read some manuals

I found an awful method forge.control.home.ControlConstructed.oa2sa(Object[]) which is supposed to return a string array of the same size.
Author = Doublestrike, exists since 12716 (that is for 2 months)
used in scenarios like this:
Second, there is a simple way to convert List<T> to T[]: declare an array of matching size and pass it as argument to toArray() method. Another option: pass there an empty array to have the function allocate array for you.
Third, once you create utility funcitons that might be useful to other classes anr their devlopers, care to find a good place for that code in forge.util package.
So the a better edition for this method would be
I also think that this forum can be a good place to ask questions on how to do something. Java questions like "how to convert list to array" will find answers here for sure, even if same question had been asked on stackoverflow decades ago.
As for me it's better to answer a simple question here than to see utility functions custom implementations in our project.
- Code: Select all
/**
* Exhaustively converts object array to string array.
* Probably a much easier way to do this.
*/
private String[] oa2sa(final Object[] o0) {
final String[] output = new String[o0.length];
for (int i = 0; i < o0.length; i++) {
output[i] = o0[i].toString();
}
return output;
}
Author = Doublestrike, exists since 12716 (that is for 2 months)
used in scenarios like this:
- Code: Select all
private String[] getEventNames() {
final List<String> eventNames = new ArrayList<String>();
eventNames.clear();
for (final QuestEvent e : Singletons.getModel().getQuestEventManager().getAllChallenges()) {
eventNames.add(e.getEventDeck().getName());
}
for (final QuestEvent e : Singletons.getModel().getQuestEventManager().getAllDuels()) {
eventNames.add(e.getEventDeck().getName());
}
return oa2sa(eventNames.toArray());
}
Second, there is a simple way to convert List<T> to T[]: declare an array of matching size and pass it as argument to toArray() method. Another option: pass there an empty array to have the function allocate array for you.
Third, once you create utility funcitons that might be useful to other classes anr their devlopers, care to find a good place for that code in forge.util package.
So the a better edition for this method would be
- Code: Select all
private String[] getEventNames() {
final List<String> eventNames = new ArrayList<String>();
final QuestEventManager qm = Singletons.getModel().getQuestEventManager();
for (final QuestEvent e : qm.getAllChallenges()) {
eventNames.add(e.getEventDeck().getName());
}
for (final QuestEvent e : qm.getAllDuels()) {
eventNames.add(e.getEventDeck().getName());
}
return eventNames.toArray(ArrayUtils.EMPTY_STRING_ARRAY);
}
I also think that this forum can be a good place to ask questions on how to do something. Java questions like "how to convert list to array" will find answers here for sure, even if same question had been asked on stackoverflow decades ago.
As for me it's better to answer a simple question here than to see utility functions custom implementations in our project.