Creation d un tableau de type connu a l instanciation
Bonjour,
J ai un probleme de genericite que je n arrive pas a resoudre.
J ai dans un fichier des donnees (de type non connu). Je veux copier ces donnees dans un tableau. Je teste donc le type de donnee :
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| public Class getType(Variable var) { // var est l object de la variable dont les donnees sont dans le fichier
try {
for (String data : getAllData(var)) {
Integer.parseInt(data);
}
return Integer.class;
} catch (NumberFormatException ex) {
}
try {
for (String data : getAllData(var)) {
Float.parseFloat(data);
}
return Float.class;
} catch (NumberFormatException ex) {
}
try {
for (String data : getAllData(var)) {
Double.parseDouble(data);
}
return Double.class;
} catch (NumberFormatException ex) {
}
return String.class;
} |
Comment alors creer un tableau de ce type:
ArrayList<quoi mettre ici> array = newArrayList<quoi mettre ici>();
Je peux bien sur faire un tableau ArrayList<?> array = newArrayList<?>(); mais j aurai aime preciser le type car j ai besoin de le connaitre plus tard.
Pour le moment j ai une variable ou lutilisateur dit si c est numerique (que je met alors double pour etre sur) ou text (String).
Merci pour vos reponses