/**
* 定义一个方法,把数组{1,2,3}拼接成一个字符串,格式为[word1#word2#word3]。
*/
/**
* 定义一个方法,把数组{1,2,3}拼接成一个字符串,格式为[word1#word2#word3]。
*/
public class ArrayToString {
public static void main(String[] args) {
int[] array = {1, 2, 3};
String str = fromArrayToString(array);
System.out.println(str);
}
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;
}
}
![](https://img-blog.csdnimg.cn/20200107225216366.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xVX0xlbw==,size_16,color_FFFFFF,t_70)