int[] num = {2,4,6,8,10}; for (int i = 0; i < 5; i++) { System.out.println(num[i]); }
int[] num = {2,4,6,8,10}; for (int x:num) { System.out.println(x); }
public class Demo { public static void main(String[] args) { int[] num = {2,4,6,8,10}; //调用print方法 print(num); } //打印数组 public static void print(int array[]){ for (int x:array){ System.out.println(x); } } }
public class Demo { public static void main(String[] args) { int[] num = {2,4,6,8,10}; //调用reverse方法 for (int x:reverse(num)){ System.out.println(x); } } public static int[] reverse(int num[]){ int[] result = new int[num.length]; //反转数组 for (int i=0,j=num.length-1;i<num.length;i++,j--){ result[j]=num[i]; } return result; } }
简单理解就是数组里边的元素是数组
以前觉得很绕的一个地方,现在突然理解了!!!
int a[][] = new int[2][3];
或者
int a[][] = {{1,2},{3,4},{5,6},{7,8}}; for (int i=0;i<a.length;i++){ for (int j=0;j<a[i].length;j++){ System.out.print(a[i][j]+" "); } }