Stuck using an old java version, so I am not able to cast my array list to a string, and was wondering how I can successfully convert my arrayList to an array. here is my code:
String racesArray[] = request.getParameterValues("races");
ArrayList races = new ArrayList();
for (int i = 0; i < racesArray.length; ++i) {
String s = racesArray[i];
if (s != null && s.length() > 0) {
races.add(s);
}
}
final String[] racesArray1 = (String[])races.toArray(new Object[races.size()]);
The above codes gives me a classCastException
EDIT: Changed to avoid confusion on variables
Try this:
String[] racesArray = { "raceWhite", "raceBlack", "raceInd", "raceAsian", "raceHaw", "raceInter", "raceUnknown" };
List races = Arrays.asList(racesArray);
String[] racesArray1 = (String[]) races.toArray(new String[races.size()]);
Take attention on the last line: new String not new Object -- this was the main reason of your problem.
Good luck with Java 1.4.2 :) and I recommend the book of Joshua Bloch "Effective Java" 1st edition. It is for Java 1.4 and touches the most important aspects of everyday programming.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With