adding in a ArrayList by casting an Object, from table of Objects, into double -
why cast (double) doesn't work ? nead add doubles arraylist.
import java.util.arraylist; import java.util.listiterator; public class vecteur extends arraylist implements cloneable{ int n; public vecteur(int n, object ... v){ this.n=n; if(v.length>=n) for(int i=0; i<n; i++) add((double)v[i]); else{ for(int i=0; i<v.length; i++){ if(i<n) add((double)v[i]); add(0); } } } }
is there particular reason not using generics?
you try making class header this:
public class vecteur<t> extends arraylist<t> implements cloneable {... then constructor:
public vecteur(int n, t... v) {... which apply class generic type constructor. in manner, can remove cast code long instantiate container syntax this:
vecteur<double> v = new vecteur<double>(); if need information on generics, check out wikipedia page:
edit/update:
error in comment expected see. can use java.lang.double wrapper class exception handling disallow trying add objects aren't castable double:
try { add(double.valueof(v[i].tostring()).doublevalue()); } catch(numberformatexception nfex) { nfex.printstacktrace(); } if don't use type of handling you'll classcastexceptions.
Comments
Post a Comment