-
数组概述
-
数组声明创建
-
数组使用
-
多维数组
-
Arrays类
-
稀疏数组
ArrayDemo01
public class SwichDemo02 { public static void main(String[] args) { String name = "秦疆"; //JDK7的新特性,表达式结果可以是字符串!!! //字符的本质是数字 //反编译 java---class(字节码文件)---反编译(IDEA) //SwichDemo02.java右键选择open in的Explorer!!! //右键选择Split Right!!! switch(name){ case"秦疆": System.out.println("秦疆"); break; case"狂神": System.out.println("狂神"); break; default: System.out.println("弄啥嘞"); } } }
Demo02
ArrayDemo02 { public static void main(String[] args) { //静态初始化:创建+赋值 int[] a = {1,2,3,4,5,6,7,8}; System.out.println(a[0]); for (int i = 0; i < a.length; i++) { System.out.println(a[i]); } // //动态初始化,包含默认初始化 // int[] b = new int[10]; // b[0] = 10; // // System.out.println(b[0]); // System.out.println(b[1]); } }
Demo03
public class ArrayDemo03 { public static void main(String[] args) { int[] arrays = {1,2,3,4,5}; //JDK1.5:没有下标 // for (int array : arrays) { // System.out.println(array); // } // printArray(arrays); int[] reverse = reverse(arrays); printArray(reverse); } //反转数组 public static int[] reverse(int[] arrays){ int[] result = new int[arrays.length]; for (int i = 0,j = result.length-1; i < arrays.length; i++,j--) { result[j] = arrays[i]; } return result; } //打印数组元素 public static void printArray(int[] arrays){ for (int i = 0; i < arrays.length; i++) { System.out.print(arrays[i]+" "); } } }
Demo04
public class ArrayDemo04 { public static void main(String[] args) { //[4][2] /* 1,2 array[0] 2,3 array[1] 3,4 array[2] 4,5 array[3] */ int[][] array={{1,2},{2,3},{3,4},{4,5}}; System.out.println(array.length); System.out.println(array[0].length); System.out.println(array[0][0]); System.out.println(array[3][1]); } //打印数组元素 public static void printArray(int[] arrays){ for (int i = 0; i < arrays.length; i++) { System.out.print(arrays[i]+" "); } } }
Demo05
public class ArrayDemo06 { public static void main(String[] args) { int[] a = {1,2,3,4,9090,3322,7917}; System.out.println(a); //打印数组元素 //System.out.println(Arrays.toString(a)); //printArray(a); Arrays.sort(a);//数组进行排序 System.out.println(Arrays.toString(a)); Arrays.fill(a,0);//数组填充 } public static void printArray(int[] a){ for (int i = 0; i < a.length; i++) { if(i==0){ System.out.print("["); } if (i== a.length-1){ System.out.print(a[i]+"]"); }else { System.out.print(a[i]+", "); } } } }
Demo06
import java.util.Arrays; public class ArrayDemo07 { public static void main(String[] args) { int[] a = {1,7,9,5,6,44,98,23,456,179}; sort(a); System.out.println(Arrays.toString(a)); } //冒泡排序 //1.比较数组中,两个相邻的元素,如果第一个数比第二个数大,我们就交换他们的位置 //2.每一次比较,都会产生出一个最大,或是最小的数字 //3.下轮则可以少一次排序! //4.依次循环,直到结束 public static int[] sort(int[] array){ int temp=0; for (int i = 0; i < array.length - 1; i++) { boolean flag = false; for (int j = 0; j < array.length-1-i; j++) { if (array[j]>array[j+1]){ temp = array[j]; array[j]=array[j+1]; array[j+1]=temp; flag = true; } } if(flag==false){ break; } } return array; } }
Demo07
public class ArrayDemo08 { public static void main(String[] args) { //1.创建一个二维数组11*11 0没有棋子 1黑棋 2.白棋 int[][] array1 = new int[11][11]; array1[1][2] = 1; array1[2][3] = 2; //输出一个原始的数组 System.out.println("输出原始的数组"); //增强For循环(局部变量:访问数组名) //增强For循环先遍历地址,再遍历地址中的值 for (int[] ints : array1) { for (int anInt : ints) { System.out.print(anInt+"\t"); } System.out.println(); } System.out.println("==============="); //1.转换为稀疏数组保存 //获取有效值的个数 int sum = 0; for (int i = 0; i < 11; i++) { for (int j = 0; j < 11; j++) { if (array1[i][j] != 0){ sum++; } } } System.out.println("有效值的个数"+sum); //2.创建一个稀疏数组的数组 int [][] array2 = new int[sum+1][3]; array2[0][0] = 11; array2[0][1] = 11; array2[0][2] = sum; //3.遍历二维数组,将非零的值,存放稀疏数组中 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]; } } } //4.输出稀疏数组 System.out.println("稀疏数组"); for (int i = 0; i < array2.length; i++) { System.out.println(array2[i][0]+"\t" +array2[i][1]+"\t" +array2[i][2]+"\t"); } System.out.println("===================="); System.out.println("还原"); //1.读取稀疏数组 int[][] array3 = new int[array2[0][0]][array2[0][1]]; //2.给其中的数组赋值 for (int i = 1; i < array2.length; i++) { array3[array2[i][0]][array2[i][1]] = array2[i][2]; } //3.打印 for (int[] ints : array3) { for (int anInt : ints) { System.out.print(anInt+"\t"); } System.out.println(); } } } //注意数组下标是否越界!!!