数组(Array)是Java 语言中内置的一种基本数据存储结构,通俗的理解,就是一组数的集合,目的是用来一次存储多个数据。数组是程序中实现很多算法的基础,可以在一定程度上简化代码的书写。
注意
一维数组:可以理解为一列多行、类型相同的数据,其中每个数据被称为数组元素;
一维数组的声明方式:
type varName[]; 或 type[] varName;(推荐)Eg:int age[]; int []age;
数组的长度一旦确定,就不能改变,数组是定长的;
错误的声明:Eg:int a[5];
数组的初始化
Java中的数组必先初始化才可以使用,所谓初始化就是为数组的数组元素分配内存,并为每个数组元素赋值;
数组初始化的两种方式
格式:数组名 = new 数组类型[]{元素1,元素2,元素3...元素n};简化语法:数组名 = {元素1,元素2,元素3...元素n};
int[] arr = new int[]{1,2,3,4,5,6};
int[] arr = {1,2,3,4,5,6};
格式:数组名 = new 数组类型[数组长度];
int arr = new int[20];
注意:设置了20长度即为只能最多保存20个元素。
package reviewDemo;/** * 声明数组及初始化 */public class Demo3 { public static void main(String[] args) { int []age = new int[10]; //动态初始化 for (int i = 0; i < age.length; i++) { age[i] = i; System.out.print(age[i]+" "); } }}
输出:0 1 2 3 4 5 6 7 8 9
Java语言的数组索引是从0开始的,也就是说数组里的第一个元素的索引是0,第二个元素的索引是1,依次可以类推。
常见操作
给数组元素赋值 数组名[索引] = 数组类型的值 ; arr[0] = 10; arr[1] = 11;
访问数组元素 数组类型 变量 = 数组名[索引]; int num = arr[0];
得到数组的长度 int len = 数组名.length; //length是数组的属性 int len = arr.length;
遍历数组(备注:length 属性和循环语句) 数组元素的索引范围(0,长度—1)
Eg:判断数组是否重复package reviewDemo;/** * 判断数组是否重复 */public class Demo3 { public static void main(String[] args) { int []age = {1,2,3,4,5,6,5}; for (int i = 0; i < age.length-1; i++) {//双层循环,定住一个,再考虑下一个! for (int j = i+1; j < age.length; j++) { if(age[i] == age[j]){ System.out.println("有重复的!"+i+" "+j); break; } } } }}
求最大值:package reviewDemo;
public class Demo4 { public static void main(String[] args) { int age[] = new int[] { 12, 26, 3, 60, 55, 6, 48, 4, 98 }; int max = age[0]; for (int i = 0; i < age.length; i++) { if (max < age[i]) { max = age[i]; } } System.out.println(max);
}}
经典用法:冒泡法排序class Bubblesort{ public static void main(String args[]) { int [] arr={5,1,6,4,2,8,9}; bubble(arr); printarray(arr);
} public static void bubble(int[] arr) { for (int i=0;i<arr.length-1 ;i++ ) { for (int y=0;y<arr.length-i-1 ; y++) //让每一次比较的元素减少,-1是为了防止数组角标越界; { if(arr[y]>arr[y+1]) //相邻两元素相比 { int temp = 0; temp = arr[y]; arr[y] = arr[y+1] ; arr[y+1] = temp; } } } } public static void printarray(int[] arr) {
for (int i=0;i<arr.length ;i++ ) { if(i!=arr.length-1) System.out.print(arr[i]+","); else System.out.println(arr[i]); } }
}
//选择排序public class Demo6 { public static void main(String[] args) { int []age = {1,2,36,363,56,95,12,32,1232,3263};
for (int i = 0; i < age.length; i++) { for (int j = i+1; j <= age.length-1; j++) { if(age[i] > age[j]){ int temp = age[i]; age[i] = age[j]; age[j] = temp; } } } System.out.println(Arrays.toString(age)); }} //输出为:[1, 2, 12, 32, 36, 56, 95, 363, 1232, 3263]
二维数组:(其实是一个一维数组,它的每一个元素又是一个一维数组),
结构:{ {1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 2, 3, 4, 5, 6, 7, 8, 9}, {1, 2, 3, 4, 5, 6, 7, 8, 9}}
初始化
int[ ][ ] arr = new int[3][2];定义了一个二维数组,其中有3个一维数组,每一个一维数组中有2个元素
int[ ][ ] arr = new int[][]{{1,2},{3,4},{5,6}};int[ ][ ] arr = {{1,2},{3,4},{5,6}};
Eg:public class Demo3 { public static void main(String[] args) { int age[][] = new int[][]{{1,2},{3,4},{5,6,7}};System.out.println(age[0].length);//2 System.out.println(age[2].length);//3}}
常用方法
static int binarySearch(type[] a, type key) 使用二分搜索法来搜索key元素在数组中的索引; 若a数组不包括key,返回负数。(该方法必须已按升序排列后调用)。
static int binarySearch(type[] a, int fromIndex, int toIndex, type key) 使用二分搜索法来搜索key元素在数组中从fromIndex到toIndex的索引; 若a数组不包括key,返回负数。(该方法必须已按升序排列后调用)。
static boolean[] copyOf(type[] original, int newLength) 复制指定的数组见下面备注
static byte[] copyOfRange(type[] original, int from, int to) 将数组的指定范围复制到一个新数组。
static boolean equals(type[] a, type[] a2) 如果两个数组长度相等和元素一一相等,则返回 true
static void fill(type[] a, type val) 将a数组所有元素都赋为val。
static void fill(type[] a, int fromIndex, int toIndex, type val) 将a数组从formIndex 到tiondex索引之间的元素都赋为val。
static void sort(type[] a) //sort(int[] arr) 对指定的数组按数字升序进行排序。
static void sort(type[] a, int fromIndex, int toIndex) 对指定数组的从formIndex 到tiondex索引之间的元素按数字升序进行排序。
static String toString(type[] a) 返回指定数组内容的字符串表示形式。多个数组元素之间用英文逗号或空格隔开。
Eg:package reviewDemo;
import java.util.Arrays;
//使用Arrays类public class Demo4 { public static void main(String[] args) { int[] age = new int[] { 12, 26, 3, 60, 55, 6, 48, 4, 98 }; System.out.println(Arrays.toString(age));//直接打印出数组的方法
int []age2 = {1,2,3,4,5,6,98,65,23}; int i = Arrays.binarySearch(age2, 98); System.out.println(i); }}
增强for循环 → for-each
for (参数类型 参数名 : 数组名) { 代码块}
Eg:package reviewDemo;
public class Demo6 { public static void main(String[] args) { int []age = {1,2,3,69,36,636,459}; for (int i : age) { System.out.println(i); } }} 这样就用for-each把数组遍历输出!