数组的定义
- 数组是相同类型数据的有序集合
- 按照一定的先后次序排列组合而成
- 每个数据称为数组的元素,可以用下标来访问
数组的声明和创建
int[] array1 = {1,2,3}; //静态初始化
//动态初始化
int[] array; //声明
array = new int[3]; //创建
//赋值
array[0] = 1;
array[1] = 2;
array[2] = 3;
//lenth属性
int i = array.lenth;
System.out.println(array[3]); //报错java.lang.ArrayIndexOutOfBoundsException: 3
java.lang.ArrayIndexOutOfBoundsException: 3
报错:数组索引超出边界异常
三种初始化
- 静态初始化
- 动态初始化
- 默认初始化
数组与对象都在堆中
数组的使用
public class Demo02 {
public static void main(String[] args) {
int[] arrays = {1,2,3,4,5};
int[] reverse = reverse(arrays);
printArray(reverse);
}
//打印数组
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[] reverse = new int[arrays.length];
for (int i = 0,j = arrays.length-1; i < arrays.length; i++,j--) {
reverse[j] = arrays[i];
}
return reverse;
}
}
多维数组
public class Demo03 {
public static void main(String[] args) {
int[][] arrays = {{1,2},{3,4},{5,6}};
System.out.println(arrays.length); //3
System.out.println(arrays[2].length); //2
}
}
Arrarys类
java.util.Arrays
文档地址:https://www.yiibai.com/java/util/java_util_arrays.html
public class Demo04 {
public static void main(String[] args) {
int[] arrays = {1,2,10,4,5,6,7,8,9,3};
System.out.println(arrays); //[I@4554617c
//打印数组元素 [1, 2, 10, 4, 5, 6, 7, 8, 9, 3]
System.out.println(Arrays.toString(arrays));
//排序 [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Arrays.sort(arrays);
System.out.println(Arrays.toString(arrays));
//数组填充 [10, 10, 10, 10, 10, 10, 10, 10, 10, 10]
Arrays.fill(arrays,10);
System.out.println(Arrays.toString(arrays));
}
}
冒泡排序
import java.util.Arrays;
public class Demo05 {
public static void main(String[] args) {
//冒泡排序:比较相邻的两个数组,若第一个大于第二个则交换位置
int[] arrays = {1,2,45,46,67,4,21,64,76,123,100};
System.out.println(Arrays.toString(arrays));//[1, 2, 45, 46, 67, 4, 21, 64, 76, 123, 100]
int[] sort = sort(arrays);
System.out.println(Arrays.toString(sort));//[1, 2, 4, 21, 45, 46, 64, 67, 76, 100, 123]
}
public static int[] sort(int[] arrays){
for (int i = 0; i < arrays.length-1; i++) {
int m = 0;
for (int j = 0; j < arrays.length-1-i; j++) {
if (arrays[j]>arrays[j+1]){
m = arrays[j];
arrays[j] = arrays[j+1];
arrays[j+1] = m;
}
}
}
return arrays;
}
}
稀疏数组
Snipaste_2021-03-30_17-43-16.png当一个数组的大部分元素都是相同的数字时,可以使用稀疏数组来保存数组
稀疏数组的处理方式:
记录数组有几行几列,有多少个不同的值
把不同的值的元素和行列及值的记录保存在小规模的数组中,从而缩小程序的规模
如下图所示:左为原始数组,右为稀疏数组
需求:编写一个五子棋游戏,
package com.array;
public class Demo06 {
public static void main(String[] args) {
//创建一个二维数组【11】【11】 0:没有棋子,1:白色棋子,2:黑色棋子
int[][] arrays = new int[11][11];
arrays[1][2] = 1;
arrays[2][3] = 2;
System.out.println("输出原始数组");
for (int[] ints : arrays) {
for (int anInt : ints) {
System.out.print(anInt+"\t");
}
System.out.println();
}
//转化为稀疏数组保存
//1.获取有效值 sum
int sum = 0;
for (int i = 0; i < 11; i++) {
for (int j = 0; j < 11; j++) {
if (arrays[i][j]!=0){
sum++;
}
}
}
System.out.println("有效值为:"+sum);
//2.创建一个稀疏数组的数组
int[][] arrays2 = new int[sum+1][3];
arrays2[0][0] = 11;
arrays2[0][1] = 11;
arrays2[0][2] = sum;
//3.遍历二维数组,将非零的值,存放在稀疏数组中
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];
}
}
}
//4.输出稀疏数组
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]);
}
//读取稀疏数组
//1.创建一个二维数组
int[][] arrays3 = new int[arrays2[0][0]][arrays2[0][1]];
//2.给其中的元素还原他的值
for (int i = 1; i < arrays2.length; i++) {
arrays3[arrays2[i][0]][arrays2[i][1]] = arrays2[i][2];
}
//3.输出还原数组
System.out.println("输出还原数组:");
for (int[] ints : arrays3) {
for (int anInt : ints) {
System.out.print(anInt+"\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
Process finished with exit code 0
网友评论