public class MyArray {
//8,1,2,3,7,4,9
public static String join(int[] array,String separator) {
String arrayString = “”;
for (int i = 0; i < array.length - 1; i++) {
arrayString+=array[i] + separator;
}return arrayString + array[array.length - 1];
}
public static String toString(int[] array) {
return join(array, “,”);
}
}
在测试类中
public class Test01 {
public static void main(String[] args) {
int[] array = {9,5,1,3,7,4,6,2,8};
System.out.println(MyArray.join(array, “,”));
System.out.println(Arrays.toString(array));
}
}
运行