一、数组概述
数组(Array),是多个相同类型数据按一定顺序排列的集合,并使用一个名字命名,并通过编号的方式对这些数据进行统一管理。
数组的常见概念:数组名 , 下标(或索引) ,元素 , 数组的长度
二、数组声明创建
1. 实例
package com.fj.array;
public class Demo01 {
public static void main(String[] args) {
//变量的类型 变量的名称=变量的值;
int[] nums;//1.定义/声明数组
nums = new int[10];//2.创建一个数组,开辟了10个空间 0-9
//3.给素组元素赋值
nums[0]=1;
nums[1]=2;
nums[2]=3;
nums[3]=4;
nums[4]=5;
nums[5]=6;
nums[6]=7;
nums[7]=8;
nums[8]=9;
nums[9]=10;
//获取下标为0的数据
System.out.println(nums[0]);
//计算所有元素的和
int num = 0;
for (int i=0;i<nums.length;i++){
num += nums[i];
}
System.out.println(num);
}
}
结果:
1
55
Process finished with exit code 0
2. 内存分析
3. 数组的初始化
- 静态初始化
package com.fj.array;
public class Demo02 {
public static void main(String[] args) {
//静态初始化:创建+赋值
int[] a = {1,2,3,4,5};
System.out.println(a);
}
}
- 动态初始化
package com.fj.array;
public class Demo02 {
public static void main(String[] args) {
//静态初始化:创建+赋值
int[] a = {1,2,3,4,5};
System.out.println(a);
//静态初始化
int[] b = new int[10];
b[0]=1;
}
}
三、数组使用
1. for 循环
package com.fj.array;
public class Demo03 {
public static void main(String[] args) {
int[] arrays = {1, 2, 3, 4, 5};
//打印全部数组元素
for (int i = 0; i < arrays.length; i++) {
System.out.println(arrays[i]);
}
System.out.println("=====================");
//计算所有元素的和
int sum = 0;
for (int i = 0; i < arrays.length; i++) {
sum += arrays[i];
}
System.out.println("sum = " + sum);
System.out.println("=====================");
//查找最大元素
int max = arrays[0];
for (int i = 1; i < arrays.length; i++) {
if (arrays[i] > max) {
max = arrays[i];
}
}
System.out.println("max = " + max);
}
}
结果:
1
2
3
4
5
=====================
sum = 15
=====================
max = 5
Process finished with exit code 0
2. 增强for循环
package com.fj.array;
// jdk1.5 以后开始的这种循环
public class Demo04 {
public static void main(String[] args) {
int[] arrays = {1, 2, 3, 4, 5};
//增强for循环
for (int array : arrays) {
System.out.println(array);
}
System.out.println("===================");
//打印数组元素方法
printArray(arrays);
System.out.println("===================");
//数组反转方法
int[] reverse =reverse(arrays);
printArray(reverse);
}
//打印数组元素
public static void printArray(int[] arrays) {
for (int i = 0; i < arrays.length; i++) {
System.out.println(arrays[i] + "");
}
}
//反转数组
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[]=arrays[i];
result[j] = arrays[i];
}
return result;
}
}
结果:
1
2
3
4
5
===================
1
2
3
4
5
===================
5
4
3
2
1
Process finished with exit code 0
四、多维数组
1. 二维数组实例:
package com.fj.array;
//多维数组
public class Demo05 {
public static void main(String[] args) {
//二维数组定义,多个一维数组组成
int[][] array = {{1,2},{2,3},{3,4},{4,5}};
// System.out.println(array[0][0]);
printArray(array);
}
//二维数组遍历
public static void printArray(int[][] array){
for (int i =0;i<array.length;i++){
for (int j=0;j<array[i].length;j++){
System.out.print(array[i][j]+" ");
}
}
}
}
结果:
1 2 2 3 3 4 4 5
Process finished with exit code 0
五、Arrays类
1. Arrays类使用说明
package com.fj.array;
//array 类
import java.util.Arrays;
public class Demo06 {
public static void main(String[] args) {
int[] a = {1,2,3,4,6532,98536,111,4,5,8};
System.out.println(a);//结果:[I@776ec8df 打印出来hashcode
//打印数组元素Arrays.toString
System.out.println(Arrays.toString(a));//结果:[1, 2, 3, 4, 6532, 98536, 111, 4, 5, 8]
//模拟Arrays.toString方法
printArray(a);//结果:[1, 2, 3, 4, 6532, 98536, 111, 4, 5, 8]
//数组排序
System.out.println();
Arrays.sort(a);
System.out.println(Arrays.toString(a));;//结果:[1, 2, 3, 4, 4, 5, 8, 111, 6532, 98536]
//数组填充
Arrays.fill(a,0);
System.out.println(Arrays.toString(a));//结果:[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
}
//自己写Arrays.toString类
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]+", ");
}
}
}
}
结果:
[I@776ec8df
[1, 2, 3, 4, 6532, 98536, 111, 4, 5, 8]
[1, 2, 3, 4, 6532, 98536, 111, 4, 5, 8]
[1, 2, 3, 4, 4, 5, 8, 111, 6532, 98536]
[0, 0, 0, 0, 0, 0, 0, 0, 0, 0]
2. 冒泡排序
- 说明:两层循环,外层表示轮数,里层依次比较
package com.fj.array;
import java.util.Arrays;
//冒泡排序
public class Demo07 {
public static void main(String[] args) {
int[] a = {1, 2, 3,89,45, 4, 65 ,5};
int[] sort = sort(a);
System.out.println(Arrays.toString(sort));
}
//冒泡排序
// 1. 比较数组中,两个相邻的元素,如果第一个比第二个大,我们就交互他们的位置
// 2. 每一次比较,都会产生一个最大,或者最小的数字
// 3. 下一轮则可以少一次排序
// 4. 依次循环直到结束
//从小到大
public static int[] sort(int[] array) {
int temp = 0;
//最多做n-1趟排序
for (int i = 0; i < array.length-1; i++) {
//对当前无序区间array[0......length-i-1]进行排序(j的范围很关键,这个范围是在逐步缩小的)
for (int j = 0; j < array.length -1-i; j++) {
if (array[j] > array[j + 1]) {//从小到大排序:把大的值交换到后面
//从小到大排序:大的给temp
temp = array[j];
//从小到大排序:把小的给大的,小的在前面,
array[j] = array[j + 1];
//从小到大排序:把temp(大)的给小的,把小的放前面,用当前大的和下一个数进行比较
array[j + 1] = temp;
}
}
}
return array;
}
}
结果:
[1, 2, 3, 4, 5, 45, 65, 89]
Process finished with exit code 0
六、稀疏数组
1. 定义:
当一个数组中大部分数据为0或者为同一值时,就可以用稀疏数组保存
2. 图例
【0】6 7 8 代表左边原始数组有6行7列8个有效数字
【1】0 3 22 代表第1个有效数字在第0行,第3列,数字为22
【2】0 6 15 代表第2个有效数字在第0行,第6列,数字为15
… 下面的以此类推
3. 练习题
- 练习题
- 解析
[0] 11 11 2 代表:共11行11列2个有效值
[1] 1 2 1 代表: 第一个有效值在第一行第二列值为1
[2] 2 3 2 代表: 第二个有效值在第二行第三列值为2 - 代码
package com.fj.array;
//稀疏数组
public class Demo08 {
public static void main(String[] args) {
//创建一个二维数组11*11 0:没有棋子 1:黑棋 2:白棋
int[][] array1 = new int[11][11];//
// [0] 11 11 2 两个有效值
array1[1][2]=1;// [1] 1 2 1 第一个有效值在第一行第二列值为1
array1[2][3]=2;//[2] 2 3 2 第二个有效值在第二行第三列值为2
System.out.println("输出原始数组array1");
//输出原始数组
for (int[] ints:array1){
for (int anInt:ints){
System.out.print(anInt+"\t");
}
System.out.println();
}
//转换为稀疏数组保存
//获取有效值得个数
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("输出array1数组有效值的个数:"+sum);
//2. 创建一个稀疏数组
int[][]array2 = new int[sum+1][3];
array2[0][0]=11;
array2[0][1]=11;
array2[0][2]=sum;
//遍历二维数组,将非0的值,存入稀疏数组中
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];
}
}
}
//打印输出系数数组
System.out.println("输出稀疏数组array2");
for (int i=0; i<array2.length;i++){
System.out.println(array2[i][0]+"\t"+array2[i][1]+"\t"+array2[i][2]+"\t");
}
//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];
}
//打印
System.out.println("输出还原输出原始数组array3");
for (int[] ints:array3){
for (int anInt:ints){
System.out.print(anInt+"\t");
}
System.out.println();
}
}
}
结果:
输出原始数组array1
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
输出array1数组有效值的个数:2
输出稀疏数组array2
11 11 2
1 2 1
2 3 2
输出还原输出原始数组array3
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
Process finished with exit code 0