一维数组的正确写法:
第一种:
int[] a = new int[5]; //or int a[] = new int[5];
第二种:
int[] b;
b=new int[5];
第三种:
int[] c={1,2,3,4,5};
第四种:
int[] d=new int[]{1,2,3,4,5};
二维数组的正确写法:
第一种:
int[][] a = new int[5][4]; //or int a[][] = new int[5][4];
第二种:
int[][] b;
b=new int[5][4];
第三种:
int[][] c={{1,2,5},{3,5,8},{7,41,56},{6,5,7},{89,56,74}};
第四种:
int[][] d=new int[][]{{1,2,5},{3,5,8},{7,41,56},{6,5,7},{89,56,74}};
数组中使用Arrays类
arrays中常用的方法:
1.排序:
Arrays.sort(数组名)
案例:
String[] hobbies = { "sports", "game", "movie" };
// 使用Arrays类的sort()方法对数组进行排序
Arrays.sort(hobbies);
System.out.println("排序后的数字");
// 使用Arrays类的toString()方法将数组转换为字符串并输出
System.out.println( Arrays.toString(hobbies));
Arrays.toString(数组名)
案例:
//2.Arrays类中toSting方法将数组转换成字符串
int[] a = new int[]{89,26,100,36,45};
System.out.println("将数字转换成字符串后的结果"+ Arrays.toString(a));