数组是具有相同数据类型的一组数据的集合。虽然基本数据类型不是对象,但由基本数据类型组成的数组却是对象,所以Java中将数组看做一个对象。
Java中数组分为一维数组,二维数组。。。。
数组作为对象允许使用关键字【new】进行内存分配。在使用数组前必须先定义数组变量所属的类型。
- int arr[]; //声明int型数组,数组中的每个元素都必须是int类型
- String str[]; //声明String型数组,每个元素都必须是String类型
- int[] arr;
- String[] str;
arr = new int[5];
使用new关键字为数组分配内存时,整型数组中各元素初始值都是0.,字符型数组中各元素初始值为‘’,String型数组的初始值为null,float型数组初始值为0.0;
- public class GetDay {
- public static void main(String[] args) {
- int days[] = new int[]{31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
- for (int i = 0; i < 12; i++) {
- System.out.println((i + 1) + "月有" + days[i] + "天");
- }
- }
- }
-
- 输出:
- 1月有31天
- 2月有28天
- 3月有31天
- 4月有30天
- 5月有31天
- 6月有30天
- 7月有31天
- 8月有31天
- 9月有30天
- 10月有31天
- 11月有30天
- 12月有31天
如果一维数组中的各个元素仍然是一个数组,那么它就是一个二维数组。
int myarr[][]={{1,2,3},{4,5}}
- public class Matrix {
- public static void main(String[] args) {
- int arr[][] = new int[3][4]; //定义二维数组
- //遍历二维数组
- for (int i = 0; i < arr.length; i++) {
- for (int j = 0; j < arr[i].length; j++) {
- System.out.print(arr[i][j] + "\t");
- }
- System.out.println();
- }
- }
- }
-
- 输出:
- 0 0 0 0
- 0 0 0 0
- 0 0 0 0
java.util包的Arrays类包含了操作数组的各种方法。
for循环语句:
- public class Matrix {
- public static void main(String[] args) {
- int arr[][] = new int[][]{{1},{2,3},{4,5,6}}; //定义二维数组
- //遍历二维数组
- 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();
- }
- }
- }
-
- 输出:
- 1
- 23
- 456
foreach循环语句:
- public class Tautog {
- public static void main(String[] args) {
- int arrs[][] = {{4, 3}, {2, 1, 0}};
- System.out.println("数组中的元素是:");
- for (int arr[] : arrs) {
- for (int ar : arr) {
- System.out.print(ar);
- }
- }
- }
- }
-
- 输出:
- 数组中的元素是:
- 43210
通过Arrays类的静态方法fill()来对数组中的元素进行替换,并不会生成新的数组。
- import java.util.Arrays;
-
- public class Swap {
- public static void main(String[] args) {
- int arrs[] = new int[5];
- Arrays.fill(arrs, 8);
- for (int arr : arrs) {
- System.out.print(arr + "\t");
- }
- }
- }
-
- 输出:
- 8 8 8 8 8
- import java.util.Arrays;
-
- public class Displace {
- public static void main(String[] args) {
- int arrs[] = new int[]{45, 12, 2, 10};
- Arrays.fill(arrs, 0, 2, 9);
- for (int arr : arrs) {
- System.out.print(arr + "\t");
- }
- }
- }
-
- 输出:
- 9 9 2 10
通过Arrays类的静态方法sort()可以实现对数组进行排序;并不会生成新的数组。
- import java.util.Arrays;
-
- public class Taxis {
- public static void main(String[] args) {
- int arrs[] = new int[]{23, 42, 12, 8, 5};
- Arrays.sort(arrs);
- for (int i = 0; i < arrs.length; i++) {
- System.out.print(arrs[i]+"\t");
- }
- }
- }
-
- 输出:
- 5 8 12 23 42
Arrays类的copyOf()方法和copyOfRange()方法可以实现对数组的复制。会生成新数组。
复制数组至指定长度;arr:要进行复制的数组, newlength:复制后新数组的长度,若新数组长度大于原数组,则用0填充(char型用null填充),会生成新数组。
- import java.util.Arrays;
-
- public class Cope {
- public static void main(String[] args) {
- int arrs[] = {23, 43, 12};
- int arrs2[] = Arrays.copyOf(arrs, 5); //复制会生成新数组
- for (int i = 0; i < arrs2.length; i++) {
- System.out.println("第" + (i + 1) + "个数是:" + arrs2[i]);
- }
- }
- }
-
- 输出:
- 第1个数是:23
- 第2个数是:43
- 第3个数是:12
- 第4个数是:0
- 第5个数是:0
将指定数组的指定长度复制到一个新数组中。arr:要进行复制的数组对象; fromIndex:指定开始复制的索引位置;toIndex:指定结束复制的索引位置。
- import java.util.Arrays;
-
- public class Repeat {
- public static void main(String[] args) {
- int arrs[] = new int[]{23, 42, 12, 84, 10};
- int newArrs[] = Arrays.copyOfRange(arrs, 0, 3);
- for (int i = 0; i < newArrs.length; i++) {
- if (i == 2) {
- System.out.print(newArrs[i]);
- } else {
- System.out.print(newArrs[i] + ",");
- }
- }
- }
- }
-
- 输出:
- 23,42,12
Arrays类的binarySearch()方法,可以使用二分搜索法来搜索指定数组,以获得指定对象。该方法返回要搜索元素的索引值。
arr:要所搜的数组,key:要搜索的值。如果key包含在数组中,则返回索引值,否则返回-1或“-”。
- int arrs[] = new int[]{4, 25, 10};
- Arrays.sort(arrs);
- int index = Arrays.binarySearch(arrs, 25);
- System.out.println("25的索引值是:" + index);
-
- 输出:
- 25的索引值是:2
在指定的索引范围内搜索元素。
- String str[]= new String[]{"ab","cd","ef","yz"};
- Arrays.sort(str);
- int index = Arrays.binarySearch(str,0,2,"cd");
- System.out.println(index);
-
- 输出:
- 1
冒泡排序过程总是将小的往前放,大的往后放,类似水中气泡上升动作。每一轮排序都将最大的数顺到了数组的后面。
基本思想:
对比相邻的两个元素值大小,满足条件就交换元素值位置,把较小的元素值移动到数组的前面。
- /**
- * 冒泡排序算法
- */
- public class BubbleSort {
- public static void main(String[] args) {
- int arrs[] = new int[]{63, 4, 24, 1, 3, 15};
- //i控制轮数
- for (int i = 1; i < arrs.length; i++) {
- //j控制每轮对比的元素个数
- for (int j = 0; j < arrs.length - i; j++) {
- if (arrs[j] > arrs[j + 1]) {
- int temp = arrs[j];
- arrs[j] = arrs[j + 1];
- arrs[j + 1] = temp;
- }
- }
- }
-
- System.out.print("{");
- for (int arr : arrs) {
- System.out.print(arr + ",");
- }
- System.out.println("}");
- }
- }
-
- 输出:
- {1,3,4,15,24,63,}
直接选择排序比冒泡排序更快
基本思想:
将指定排序位置与其他数组元素分别对比,如满足条件就交换元素位置。每一轮从待排序的数据中选出最小(最大)的一个元素,顺序地放到已排好序的数列的最后,直到全部数据排完。
用一个临时变量记录当前已知的最大数索引值,每次与这个最大数索引值对比,如果比这个临时变量大,则这个临时变量就是这个数。
- public class SelectSort {
- public static void main(String[] args) {
- int arrs[] = new int[]{63, 4, 24, 1, 3, 15};
- for (int i = 1; i < arrs.length; i++) {
- int index = 0;
- for (int j = 1; j <= arrs.length - i; j++) { //j=0已被index占有,所以j从1开始;每轮最后一个数也要比较,所以j<=
- if (arrs[j] > arrs[index]) {
- index = j;
- }
- }
- int temp = arrs[arrs.length - i]; //把每轮的最后一个元素保存在临时变量中
- arrs[arrs.length - i] = arrs[index]; //把每轮已比较出的最大索引的值赋值给每轮的最后一个数
- arrs[index] = temp; //将每轮最后一个数与最大值数交换位置
- }
-
- System.out.print("{");
- for (int arr : arrs) {
- System.out.print(arr + ",");
- }
- System.out.print("}");
- }
- }
-
- 输出:
- {1,3,4,15,24,63,}
反转排序就是把原有的数组内容以相反的顺序重新排列。
基本思想:
把数组最后一个元素与第一个元素交换位置,倒数第二个元素与第二个元素交换位置,以此类推。
- public class ReverseSort {
- public static void main(String[] args) {
- int arrs[] = new int[]{1, 2, 3, 4, 5, 6};
- for (int i = 0; i < arrs.length / 2; i++) {
- int temp = arrs[i];
- arrs[i] = arrs[arrs.length - i - 1];
- arrs[arrs.length - i - 1] = temp;
- }
- System.out.print("{");
- for (int arr : arrs) {
- System.out.print(arr + ",");
- }
- System.out.print("}");
- }
- }
-
- 输出:
- {6,5,4,3,2,1,}