package com.day05.www; /*设计一个方法用于输出任意整型数组的内容,要求输出成如下格式: “该数组内容为:[11, 22, 33, 44, 55]” */ public class Demo03 { public static void main(String[] args) { int[] arr = {11, 22, 33, 44, 55}; int[] arr2 = {}; print(arr); System.out.println("---------------------"); print(arr2); } public static void print(int[] arr) { if (arr != null && arr.length > 0) { System.out.print("["); for (int i = 0; i < arr.length; i++) { System.out.print(i < arr.length - 1 ? (arr[i] + ", ") : (arr[i])); /*上面这条语句可理解为: if(i == arr.length - 1) System.out.print(arr[i]); else System.out.print(arr[i] + ", ");*/ } System.out.println("]"); } else System.out.println("当前数组中没有对象,其地址是:null"); } }
06-21
356
07-19
752