I wanted to convert the Arraylist <String> to array double and i used the following code:
String [] n = (String[]) lines.toArray();
String[] array = lines.toArray(new String[lines.size()]);
double[] nums = new double[n.length];
for (int i= 0 ; i< nums.length; i++){
nums[i] = Double.parseDouble(n[i]);
but then i have the following error:
Exception in thread "main" java.lang.ClassCastException: class [Ljava.lang.Object; cannot be cast to class [Ljava.lang.String; ([Ljava.lang.Object; and [Ljava.lang.String; are in module java.base of loader 'bootstrap')
could anybody help me?
linesdefined?String [] n = (String[]) lines.toArray();(which seems to be cause of the exception and strangely you provided code with solution to that error in next lineString[] array = lines.toArray(new String[lines.size()]);)?String[]makes sense.linesis anObject[]and you simply cast it toString[].