定义
相同类型数据的有序集合。
每一个数据称作一个数组元素,通过一个下标来访问。
声明 创建
//声明数组
int[] nums;//首选
//int nums[];
//创建数组
nums = new int[10];
//声明+创建
int[] nums2 = new int[10];
//赋值
nums[0] = 1;
nums[1] = 2;
内存分析
![](https://img-blog.csdnimg.cn/20200326003152564.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xlc2xpZV9XYW9uZw==,size_16,color_FFFFFF,t_70)
![](https://img-blog.csdnimg.cn/20200326003523303.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0xlc2xpZV9XYW9uZw==,size_16,color_FFFFFF,t_70)
三种初始化
//静态初始化 创建+赋值
int[] a = {1,2,3,4,5}
//动态初始化 包含默认初始化
int[] b = new int[10];
b[0] = 10;
特点
- 数组也是对象。数组元素相当于对象的成员变量。
- 长度确定,不可变。若越界,则报:ArrayIndexOutofBounds
数组使用
- For-Each循环
- 数组作方法入参
- 数组作返回值
public class ArrayDemo {
public static void main(String[] args) {
int[] arrays = {1,2,3,4,5};
int[] nums = {1,2,3,4,5};
//求和
int sum = 0;
for (int i = 0; i < nums.length; i++) {
sum =sum+nums[i];//sum += nums[i]
}
System.out.println("总和:"+sum);
//查找最大值
int max = nums[0];
for (int i = 0; i < nums.length; i++) {
if(nums[i]>max)
max = nums[i];
}
System.out.println(max);
//没有下标
for (int i : nums) {
System.out.println(i);
}
printArray(reverse(arrays));
}
//打印数组元素
public static void printArray(int[] arrays) {
for (int i = 0; i < arrays.length; i++) {
System.out.print(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[j]=arrays[i];
}
return result;
}
}
多维数组
public class ArrayDemo2 {
public static void main(String[] args) {
//[4][2]
int[][] array = {{1,2},{2,3},{4,5},{5,6}};
for (int i = 0; i < array.length; i++) {
for (int j = 0; j < array[i].length; j++) {
System.out.println(array[i][j]);
}
}
}
}
Arrays类
import java.util.Arrays;
public class ArrayDemo3 {
public static void main(String[] args) {
int[] a = {1,5,97,21,44};
//打印数组元素
System.out.println(Arrays.toString(a));
//排序:升序
Arrays.sort(a);
System.out.println(Arrays.toString(a));
//数组填充
Arrays.fill(a,2,3,0);
System.out.println(Arrays.toString(a));
}
}
[1, 5, 97, 21, 44]
[1, 5, 21, 44, 97]
[1, 5, 0, 44, 97]
冒泡排序
两层循环,外层冒泡轮数,里层依次比较。
import java.util.Arrays;
public class ArrayDemo4 {
public static void main(String[] args) {
int[] a = {1,5,2,7,9};
int[] sort = sort(a);
System.out.println(Arrays.toString(sort));
}
//冒泡排序
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+1]>array[j]) { //从大到小
temp = array[j];
array[j]=array[j+1];
array[j+1]=temp;
flag = true;
}
}
if (flag == false) {//已经排好了顺序
break;
}
}
return array;
}
}
稀疏数组
一个数组中大部分元素为0,或为同一值的数组时,可用稀疏数组保存。
处理方式:
- 记录数组一共有几行几列,有多少个不同值。
- 把具有不同值的元素和行列及值记录在一个小规模的数组中,从而缩小程序规模。
public class Demo {
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;
//输出原始数组
for (int[] is : array1) {
for (int i : is) {
System.out.print(i+" ");
}
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] = array1.length;
array2[0][1] = array1.length;
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[] is : array2) {
for (int i : is) {
System.out.print(i+"\t");
}
System.out.println();
}
//还原
int[][] array3 = new int[array2[0][0]][array2[0][1]];
for (int i = 1; i < array2.length; i++) {
array3[array2[i][0]][array2[i][1]] =array2[i][2];
}
//还原数组
for (int[] is : array3) {
for (int i : is) {
System.out.print(i+" ");
}
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
学习视频:狂神说Java