数组遍历 int[] arr = {98,23,16,35,72}; 写一个对象数组操作的功能,实现把数组中的数据按照如下格式返回:
* "[98, 23, 16, 35, 72]"
*/
public static String printArray(int[] arr) {
String s = "";
for (int i = 0; i < arr.length; i++) {
if (i == arr.length - 1) {
s += arr[i];
}
else
s += arr[i] + ",";
}
return "[" + s + "]";
}
如果不加else 输出结果是:"[98, 23, 16, 35, 7272]",求解for循环里if 和else如何是好 |
|