数组的定义
数组的声明
数组的特点
数组的使用
public class Demo02 { public static void main(String[] args) { int[] arrays = {1,2,3,4,5}; //JDK1.5 无下标 For-each的使用 for(int array:arrays){ System.out.println(array); } //数组入参 printArray(arrays); int[] reverse = reverseArray(arrays); printArray(reverse); } //打印数组元素 public static void printArray(int[] arrays){ for (int i=0;i<arrays.length;i++){ System.out.print(arrays[i]+"\t"); } } //反转数组 public static int[] reverseArray(int[] arrays){ int[] result = new int[arrays.length]; for(int i =0,j=arrays.length-1;i<arrays.length;i++,j--){ result[j] = arrays[i]; } return result; } }
public class Demo01 { //要使用数组必须先声明 public static void main(String[] args){ int[] nums = {1,2,3,4,5}; //打印全部的数组元素 for(int i =0;i<nums.length;i++){ System.out.println(nums[i]); } //计算所有元素的和 int sum=0; for(int i=0;i<nums.length;i++){ sum = nums[i]+sum; } //查找最大值 int max = nums[0]; for (int i =0 ;i<nums.length;i++){ if(max<nums[i]){ max=nums[i]; } } System.out.println("MAX:"+max); } }
多维数组
Arrays类
冒泡排序
public class Demo05 {
public static void main(String[] args){
int[] arrays = {1,3,2,1,1};
sort(arrays);
}
public static void sort(int[] array){
for(int i=0;i<array.length-1;i++){
for (int j=0;j< array.length-1-i;j++){
int temp = 0;
if(array[j]>array[j+1]){
temp = array[j];
array[j] = array[j+1];
array[j+1]=temp;
}
}
}
printArray(array);
}
public static void printArray(int[] arrays){
for (int i=0;i<arrays.length;i++){
System.out.print(arrays[i]+"\t");
}
}
}
选择排序
package array;
public class Demo06 {
public static void main(String[] args) {
int[] arrays = {1,2,3,5,1,2,3};
arrays = sort(arrays);
printArray(arrays);
}
public static int[] sort(int[] arrays){
for (int i=0;i<arrays.length-1;i++){
for (int j=i+1;j<arrays.length;j++){
int temp = 0;
if(arrays[i]>arrays[j]){
temp = arrays[i] ;
arrays[i] = arrays[j];
arrays[j] = temp;
}
}
}
return arrays;
}
public static void printArray(int[] arrays){
for (int i=0;i<arrays.length;i++){
System.out.print(arrays[i]+"\t");
}
}
}
稀疏数组
![]()
package array; public class Demo07 { public static void main(String[] args) { //创建一个二维数组 11*11 0:没有棋子,1:黑棋,2:白棋 int[][] array1 = new int[11][11]; array1[1][2] = 1; array1[2][3] = 2; //输出棋盘 System.out.println("输出原始值"); for (int[] ints : array1){ for (int aint:ints){ System.out.print(aint+"\t"); } System.out.println(); } //转化为稀疏数组 //获取有效值个数 int sum = 0; for(int i =0;i<array1.length;i++){ for (int j=0;j<array1.length;j++){ if(array1[i][j]!=0){ sum++; } } } System.out.println("有效值个数为:"+sum); //创建一个稀疏数组的数组 int[][] array2 = new int[sum+1][3]; array2[0][0] = 11; array2[0][1] = 11; array2[0][2] = sum; int count =0; for (int i=0;i<array1.length;i++){ for(int j=0;j<array1[i].length;j++){ if(array1[i][j]!=0){ count++; array2[count][0]=i; //行 array2[count][1]=j; //列 array2[count][2]=array1[i][j]; //值 } } } for (int i = 0;i<array2.length;i++){ System.out.println(array2[i][0]+"\t"+array2[i][1]+"\t"+array2[i][2]+"\t"); } //还原稀疏数组 创建新数组 int[][] array3 = new int[array2[0][0]][array2[0][1]]; //1.读取稀疏数组 从1开始,因为o行为头部信息 for(int i=1;i<array2.length;i++){ array3[array2[i][0]][array2[i][1]]= array2[i][2]; } for (int[] ints : array3){ for (int aint:ints){ System.out.print(aint+"\t"); } System.out.println(); } } }