Je crée une méthode pour retourner les valeurs d'un tableau. Mais j'ai un problème.java obtenir toutes les valeurs d'un tableau dans une méthode
Supposons que les valeurs du tableau sont: 771 11 141 1 11 79 7
arr [0] = 771, arr [1] = 11, arr [2] = 141, ... arr [n]
Ensuite, c'est ma méthode:
public static String split_in_string(int [] an){
int ans;
String ansl = "";
for(int i = 0; i < an.length; i++){
ans = an[i];
ansl = Integer.toString(ans);
return ansl;
}
return ansl;
}
Si je ci-dessous une partie dans la méthode principale:
int ans;
String ansl = "";
for(int i = 0; i < an.length; i++){
ans = an[i];
ansl = Integer.toString(ans);
System.out.println(ansl+" ");
}
Il fonctionne bien. Mais, si j'utilise la méthode split_in_string(), elle n'affiche que la première valeur du tableau.
Je me attendais 771 11 141 1 11 79 7
que le résultat, mais je reçois seulement 771
Qu'est-ce qui ne va pas dans ma méthode split_in_string()? Comment puis-je le réparer?
En split_in_string: ansl = Inte ger.toString (ans); doit être remplacé par: ansl + = Integer.toString (ans); – slaadvak