Salve a tutti! Ho notato un comportamento strano con questo apparentemente semplice codice
Praticamente ho bisogno di convertire un array di caratteri (chars) in una stringa (output).codice:public String method1(){ /*..............*/ //chars array di caratteri non vuoto String output = ""; for(int j=0; j<chars.length; j++){ output = output+chars[j]; } return output; }
La stringa output viene poi memorizzata in un array di stringhe words
E' l'array a comportarsi in maniera strana. Se voglio stampare tutte le parole di wordscodice://words array di stringhe j++; words[j] = method1();
In questo modo:
Con words = {ciao, come, stai}codice:for(int i=0; i<words.length; i++){ System.out.println(i+" "+words[i]); }
Output:
0 ciao
1 come
2 stai
Ma in quest'altro modo:
Con words = {ciao, come, stai}codice:for(int i=0; i<words.length; i++){ System.out.print(i+" "+words[i]+" "); }
Output:
0 ciao
Perche' ?? E il resto che fine ha fatto?
Riesce a stamparli bene se si aggiunge lo \n ma non posso stamparli sulla stessa linea.
Credo che questo problema generi anche altri errori in altre parti del programma piu' complicate.

Rispondi quotando
