Como posso converter a array “words” (de multiplos valores) mostrada no codigo abaixo para uma simples string?
public class SortName {
public static void main(String[] args) {
String[] words = {"java","sortpgm","this","is","an","example","of","a","sort","program"};
for (int i = 0; i < 11; i++){
System.out.print(words[i] + "\n");
}
}
}
public static void main(String[] args) {
String[] words = {"java","sortpgm","this","is","an","example","of","a","sort","program"};
String result = "";
for (int i = 0; i < words.length; i++){
result += words[i];
}
System.out.println(result);
}
[quote=mduques] public static void main(String[] args) {
String[] words = {"java","sortpgm","this","is","an","example","of","a","sort","program"};
String result = "";
for (int i = 0; i < words.length; i++){
result += words[i];
}
System.out.println(result);
} [/quote]
Dica: Utilize StringBuilder para ‘somar’ as String’.
public static void main(String[] args) {
String[] words = {"java","sortpgm","this","is","an","example","of","a","sort","program"};
StringBuilder str = new StringBuilder();
for (int i = 0; i < words.length; i++){
str.append(words[i]);
}
System.out.println(str.toString());
}