/ *
Title:
define a method, the array {1,2,3} assembled into a string in the specified format. Reference to the following format: [word1 # word2 # word3] .
analysis:
- First prepare a int [] array content is: 1,2,3
- Define a method for the array into a string
of three elements
Return Value Type: String
method name: fromArrayToString
list of parameters: int [] - Format: [word1 # word2 # word3]
use: for loop, string concatenation, the word has a word before each array element, using a # separate, distinguish between what is not the last - Method call, return value, and prints the resulting string
* /
public class Demo06StringPractise {
public static void main(String[] args) {
int[] array = {1, 2, 3};
String result = fromArrayToString(array);
System.out.println(result);
}
public static String fromArrayToString(int[] array) {
String str = "[";
for (int i = 0; i < array.length; i++) {
if (i == array.length-1){
str += "word" +array[i] + "]";
}else {
str += "word" + array[i] + "#";
}
}
return str;
}
}