一、介绍
数组是相同类型数据的有序集合
二、数组的声明和创建
三、数组的三种初始化
3.1、静态初始化
3.2、动态初始化
3.3、默认初始化
package com.massimo.charpter7;
public class Test08 {
public static void main(String[] args) {
//静态初始化
int[] array = {1 , 2 , 3};
//动态初始化
int[] array2 = new int[3];
array2[0] = 4;
array2[1] = 5;
array2[2] = 6;
//默认初始化
int[] array3 = new int[3];
//循环输出各个数组
for (int item : array){
System.out.print(item + "\t");
}
System.out.println("\n" + "================");
for (int item : array2){
System.out.print(item + "\t");
}
System.out.println("\n" + "================");
for (int item : array3){
System.out.print(item + "\t");
}
}
}
数组是引用类型,它的元素相当于类的实例变量,因此数组一经分配空间,其中的每个元素也被按照实例变量同样的方法被隐式初始化。
四、Arrays类
数组的工具类,方便我们操作数组。它是java.util包下的。
Arrays类中的方法都是static修饰的静态方法,在使用的时候可以直接用类名进行调用。
常用方法:
- 打印数组中的元素:toString();
- 排序:sort();
package com.massimo.charpter7;
import java.util.Arrays;
public class Test08 {
public static void main(String[] args) {
int[] array = {12 , 34 , 433 , 53 ,34 , 5443 , 645 ,234};
System.out.println(Arrays.toString(array));
System.out.println("===========================");
Arrays.sort(array);
System.out.println(Arrays.toString(array));
}
}
五、冒泡排序
冒泡排序无疑是最为出名的排序算法之一。
两层循环:
外层冒泡轮数,里层依次比较。
package com.massimo.charpter7;
import java.util.Arrays;
public class Test08 {
public static void main(String[] args) {
int[] array = {12, 34, 433, 53, 34, 5443, 645, 234};
int temp = 0;
boolean flag = false;
for(int i = 0 ; i < array.length ; i++) {
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;
}
}
}
System.out.println(Arrays.toString(array));
}
}
六、稀疏数组
稀疏数组的处理方式:
- 记录数组一共有几行几列,有多少不同值;
- 把具有不同值的元素和行列以及值记录在一个小规模的数组中,从而缩小程序的规模。
案例:五子棋
需求:编写五子棋游戏中,有存盘退出和续上盘的功能。
分析问题:因为该二维数组的很多值是默认值0,因此记录了很多没有意义的数据。
解决:稀疏数组。
package com.massimo.charpter7;
import java.util.Arrays;
public class Test08 {
public static void main(String[] args) {
int[][] arrays = new int[11][11];
arrays[1][2] = 1;
arrays[2][3] = 2;
System.out.println("输出原始数组:");
for(int[] ants : arrays){
for(int x : ants) {
System.out.print(x + "\t");
}
System.out.println();
}
System.out.println("=================================================");
//转换为稀疏数组
//获取有效值的个数
int sum = 0;
for(int i = 0 ; i < arrays.length ; i++) {
for(int j = 0 ; j < arrays[i].length ; j++) {
if(arrays[i][j] != 0) {
sum++;
}
}
}
System.out.println("有效值的个数为:" + sum);
//创建一个稀疏数组的数组
int[][] arrays2 = new int[sum+1][3];
arrays2[0][0] = 11;
arrays2[0][1] = 11;
arrays2[0][2] = sum;
//遍历二维数组,将非0的值存放到稀疏数组中
int count = 0;
for(int i = 0 ; i < arrays.length ; i++) {
for(int j = 0 ; j < arrays[i].length ; j++) {
if(arrays[i][j] != 0) {
count++;
arrays2[count][0] = i;
arrays2[count][1] = j;
arrays2[count][2] = arrays[i][j];
}
}
}
//输出稀疏数组
System.out.println("稀疏数组:");
for(int i = 0 ; i < arrays2.length ; i++) {
System.out.println(arrays2[i][0] + "\t" + arrays2[i][1] + "\t" + arrays2[i][2] + "\t");
}
//还原稀疏数组
System.out.println("还原");
int[][] arrays3 = new int[arrays2[0][0]][arrays2[0][1]];
for(int i = 1 ; i < arrays2.length ; i++) {
arrays3[arrays2[i][0]][arrays2[i][1]] = arrays2[i][2];
}
System.out.println("输出还原数组:");
for(int[] ants : arrays3){
for(int x : ants) {
System.out.print(x + "\t");
}
System.out.println();
}
}
}
输出原始数组:
0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
=================================================
有效值的个数为:2
稀疏数组:
11 11 2
1 2 1
2 3 2
还原
输出还原数组:
0 0 0 0 0 0 0 0 0 0 0
0 0 1 0 0 0 0 0 0 0 0
0 0 0 2 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0
0 0 0 0 0 0 0 0 0 0 0