day_06 数组
1. 数据结构
计算机存储和组织数据的一种方式,根据存储的方式不同,操作特性也就不同
合理选择数据结构能够有效的提升存储效率和运行效率
常见数据结构 ;
数组,链表,散列表,红黑树,B+树,二叉树等…
2. 数组
-
数组在内存空间中是连续存储的,并且有独一无二的下标(索引) , 下标从0 开始
-
但是数组长度一旦确定,不能更改,所以数组不能做添加删除操作
-
想要做添加和删除,需要重新创建 一个新的数组空间,把原来的数据复制到新空间中,在复制过程中,可以添加和删除
-
数组有指定下标,并且是连续存储,通过内存地址偏移,可以快速定位到我们要操作的数据.
-
数组 : 添加删除慢 , 查询更改快
-
数组声明 :
-
静态声明 : 在已知数组中每个元素的情况下,使用静态声明
-
数据类型[] 变量名 = {值1 , 值2 , 值3…};
-
数据类型 变量名[] = {值1 , 值2 , 值3…};
-
int[] a = {1,2,3,4}; int一维数组
-
int[][] b = {{1,2,3},{2,3,4},{4,3,2}}; int二维数组
-
数组可以多维,但是每一维的类型必须一致
-
动态声明 : 在不知道数组中每个元素的情况下,使用动态声明,需要指定长度,然后用对应默认值占位
-
数据类型[] 变量名 = new 数据类型[长度];
-
int[] a = new int[18];
-
boolean[] b = new boolean[10];
-
int[][] c = new int【3】【5】; 二维数组中有3个一维数组,每个一维数组中有5个数据
-
c = {
-
{0,0,0,0,0},
-
{0,0,0,0,0},
-
{0,0,0,0,0},
-
};
-
int[][][] d = new int 【4】【3】【5】;
-
整数 : 0 , 小数 : 0.0 , 字符 : \u0000 , 布尔型 : false , 引用类型 : null
public class Array_01 {
public static void main(String[] args) {
// 静态声明
int[] arr1 = {1,2,3,1,2,3,4,5,6,'x'};
// 动态声明
int[] arr2 = new int[10];
// 查询 : 数组[索引/下标]
System.out.println(arr1[0]);
// length属性 是数组的长度
System.out.println(arr1.length);
// 最后一个元素
System.out.println(arr1[ arr1.length - 1 ]);
// 更改 : 数组[下标] = 值;
arr1[0] = 100;
System.out.println(arr1[0]);
System.out.println("==========");
// 遍历 : 就是把数据都拿出来
for (int i = 0; i < arr1.length; i++) {
System.out.println(arr1[i]);
}
// java.lang.ArrayIndexOutOfBoundsException: 10
// 下标越界异常,
// System.out.println(arr1[10]);
int[] arr3 = null;
// java.lang.NullPointerException
// 因为数组是null,压根没有数组,不能使用
System.out.println(arr3[0]);
int[] arr4 = {};
// 下标越界
System.out.println(arr4[0]);
}
}
数组赋值
public class Array_04 {
public static void main(String[] args) {
int[] src = {2,3,4,5,6,7,8,9};
int[] dest = {11,12,13,14,15,16,17,18,19};
// 第一个参数 是 源数组 , 2 是源数组起始位置(包含) , 3 目标数组 , 4 目标数组起始位置(包含) , 5 替换个数
System.arraycopy(src, 2, dest, 3, 3);
for (int i = 0; i < dest.length; i++) {
System.out.println(dest[i]);
}
// 增强for循环 foreach
// for(数据类型 变量 : 数组){} 类型一般是数组中元素的类型,把数组中每个元素拿出来给了这个变量
for(int element : dest){
System.out.println(element);
}
}
}
二维数组遍历
public class Array_05 {
public static void main(String[] args) {
// 静态声明
int[][] arr1 = {
{1,2,3},
{1,4,6},
{12,41,11,12,3,45}
};
// 动态声明 有3个一维数组,每个一维数组中有4个元素
int[][] arr2 = new int[3][4];
// 获取第一个元素
int[] arr1_0 = arr1[0];
int arr1_00 = arr1_0[0];
System.out.println(arr1_00);
// [第几个一维数组][第几个元素]
System.out.println( arr1[0][0] );
// 更改
arr1[0][0] = 100;
arr2[1][2] = 1;
// 遍历
for (int i = 0; i < arr2.length; i++) {
// int[] arr = arr2[i];
for (int j = 0; j < arr2[i].length; j++) {
System.out.print(arr2[i][j] + " ");
}
System.out.println();
}
}
}
动态初始化二维数组中一维数组的元素个数
public class Array_06 {
public static void main(String[] args) {
// 有九个一维数组,每个一维数组中有四个元素
int[][] arr1 = new int[9][4];
// 有九个一维数组,并且数组都是空的
int[][] arr = new int[9][];
// 单独初始化每一个一维数组中的元素个数
for (int i = 0; i < arr.length; i++) {
arr[i] = new int[i+1];
}
for (int i = 0; i < arr.length; i++) {
for (int j = 0; j < arr[i].length; j++) {
System.out.print(arr[i][j]+" ");
}
System.out.println();
}
}
}
接收控制台输入
public class Array_07 {
public static void main(String[] args) {
// 创建对象
Scanner sc = new Scanner(System.in);
System.out.println("xxxx");
// 程序执行到这里 就不再执行,等待用户输入
// 空格是分隔符 , 回车表示输入完毕
String s = sc.next();
// 接收一行,不需要分隔符
// String s = sc.nextLine();
// 接收int类型,但是必须是纯数字,分隔符是空格
// int s = sc.nextInt();
// 接收double类型,但是必须是纯数字,分隔符是空格 , 可以有一个小数点
// double s = sc.nextDouble();
System.out.println(s);
}
}
交换变量的值的方法
public class Array_01 {
public static void main(String[] args) {
int x = 2;
int y = 3;
// 1 中间变量(开发常用)
// int temp = x;
// x = y;
// y = temp;
// 2 位移运算交换(面试用)
// ^ : 转换为二进制,每位进行异或,相同为0,不同为1
// 2 0000 0010
// 3 0000 0011
// x = x ^ y; // x = 0000 0001
// y = x ^ y; // y = 0000 0010
// x = x ^ y; // x = 0000 0011
// 3 加减运算
x = x + y; // x=5
y = x - y; // y = 2
x = x - y; // x = 3
System.out.println("x : " + x + " , y : " + y);
}
}
数组排序
1.冒泡排序
-
1 比较相邻的两个元素,如果第一个比第二个大,就交换位置
-
2 对每一对相邻元素做同样的工作,比较完一轮之后,最后一个一定是最大的哪个
-
3 在对以上元素重复该步骤,除了最后一个
-
4 一直到没有一对元素需要比较,终止
-
嵌套循环,外层循环决定循环了多少轮 , 内层循环决定每轮比较的次数
public class Array_02_BubbleSort {
public static void main(String[] args) {
int[] arr = {3,2,4,5,1};
bubbleSort(arr);
for (int i : arr) {
System.out.println(i);
}
}
public static void bubbleSort(int[] arr){
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length-1-i; j++) {
if (arr[j] > arr[j+1]) {
int temp = arr[j];
arr[j] = arr[j+1];
arr[j+1] = temp;
}
}
}
}
}
2.选择排序
-
1 每次都把最小的放到左边
-
先拿出第一个 假设是最小的,然后挨个和后面所有的进行比较,如果有比他小的,就保存对应的下标
-
2 比较完之后,就保存了最小的这个下标,然后判断是否和 第一个相同,不同就换位
public class Array_03_SelectSort {
public static void main(String[] args) {
int[] arr = {3,2,4,5,1};
selectSort(arr);
for (int i : arr) {
System.out.println(i);
}
}
public static void selectSort(int[] arr){
for (int i = 0; i < arr.length; i++) {
// 假设当前位 是最小的
int min = i;
for (int j = i+1; j < arr.length; j++) {
// 如果有比min小的,就把该下标赋值给min
if (arr[min] > arr[j]) {
min = j;
}
}
// 判断 是否一致,如果一致 说明i就是最小的,就继续下次循环
// 如果不一致说明有比i小的,就换位
if (min != i) {
int temp = arr[i];
arr[i] = arr[min];
arr[min] = temp;
}
}
}
}
3.api排序
public class Array_04_API {
public static void main(String[] args) {
int[] arr = { 3, 2, 4, 5, 1 };
// 直接调用即可
Arrays.sort(arr);
for (int i : arr) {
System.out.println(i);
}
}
}