基本概念
- 数组是存放相同类型数据的有序集合,通过下标或者索引来使用数组
声明创建
- 声明数组对象
- int[] a; 声明整型一维数组引用a
- 创建数组对象
- 创建时必须给定第一维的长度,否则系统无法分配空间,导致编译失败
- new int[4]; 创建整型一维数组对象
- a = new int[4]; 将引用a指向长度为4的整型一维数组对象
- 二维数组声明
- int a [][] = new int[行的个数][列的个数]
- 对于数组对象来说,对象一旦被创建,大小不可改变,
- 也就是说数组的length属性是public,final,int的
数组初始化
- 数组初始化包括默认初始化,循环初始化,枚举初始化
- int[] day = {1,2,3,4,5};
- int[] day = new int[]{1,2,3,4,5};//枚举初始化
- int[][] day = {{1,12},{2,12}{3,12},{4,12},{5,12}};
- int[][] day = new int[][]{{1,12},{2,12}{3,12},{4,12},{5,12}};
交换两个原生数据类型的数据只能采用数组来交换
- package com.itlwc;
- public class Test {
- public static void main(String[] args) {
- int[] i = { 1, 2 };
- swap(i);
- System.out.println(i[0]);
- System.out.println(i[1]);
- }
- public static void swap(int[] i) {
- int temp = i[0];
- i[0] = i[1];
- i[1] = temp;
- }
- }
数组遍历
- package com.itlwc;
- public class Test {
- public static void main(String[] args) {
- int[] a = new int[] { 1, 2, 3, 4, 5 };
- // 遍历1
- for (int i = 0; i < a.length; i++) {
- System.out.print(a[i]);
- }
- System.out.println();
- // 遍历2
- for (int i : a) {
- System.out.print(i);
- }
- }
- }
数组复制
- package com.itlwc;
- import java.util.Arrays;
- public class Test {
- public static void main(String[] args) {
- int[] a = new int[] { 1, 2, 3, 4, 5 };
- int[] b = new int[a.length];
- // 方法一
- System.arraycopy(a, 0, b, 0, a.length);
- // 方法二
- b = Arrays.copyOf(a, a.length);
- // 方法三
- b = Arrays.copyOfRange(a,0,a.length);
- }
- }
数组排序
- package com.itlwc;
- import java.util.Arrays;
- public class Test {
- public static void main(String[] args) {
- int[] a = new int[] { 1, 2, 3, 4, 5 };
- int[] b = new int[] { 1, 2, 3, 4, 5 };
- // 方法一
- Arrays.sort(a);
- // 方法二
- Arrays.sort(a, 0, a.length);
- // 方法三
- mySort(b);
- }
- public static void mySort(int[] b) {
- for (int i = 0; i < b.length; i++) {
- for (int j = 0; j < b.length; j++) {
- if (b[i] > b[j]) {
- int temp = 0;
- temp = b[i];
- b[i] = b[j];
- b[j] = temp;
- }
- }
- }
- }
- }
搜索指定元素
- package com.itlwc;
- import java.util.Arrays;
- public class Test {
- public static void main(String[] args) {
- int[] a = new int[] { 1, 2, 3, 4, 5 };
- // 方法一
- Arrays.binarySearch(a, 1);
- // 方法二
- Arrays.binarySearch(a, 0, a.length, 1);
- }
- }
比较数组的元素
- package com.itlwc;
- import java.util.Arrays;
- public class Test {
- public static void main(String[] args) {
- int[] a = new int[] { 1, 2, 3, 4, 5 };
- int[] b = new int[] { 1, 2, 3, 4, 5 };
- Arrays.equals(a, b);
- }
- // 比较的方法
- public static boolean equals(int[] a, int[] b) {
- if (a == null || b == null)
- return false;
- if (a.length != b.length)
- return false;
- for (int i = 0; i < a.length; i++)
- if (a[i] != b[i])
- return false;
- return true;
- }
- }
二分查找
- package com.itlwc;
- public class Test {
- public static void main(String[] args) {
- int[] a = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
- int value = 9;
- System.out.println(new Test().get(a, value));
- }
- public int get(int[] a, int value) {
- int min = 0;
- int max = a.length - 1;
- int middle;
- while (min <= max) {
- middle = (min + max) / 2;
- if (a[middle] == value) {
- return middle;
- }
- if (value < a[middle]) {
- max = middle - 1;
- }
- if (value > a[middle]) {
- min = middle + 1;
- }
- }
- return -1;
- }
- }