【JAVA数组最详细讲解(完整版)】

小咪咪 2023-01-10 10:27 291阅读 0赞

数组

  • 1 数组概述
  • 2 一维数组的创建与使用
    • 2.1 创建一维数组
    • 2.2 初始化一维数组
    • 2.3 使用一维数组
  • 3 二维数组的创建与使用
    • 3.1 创建二维数组
    • 3.2 初始化二维数组
    • 3.3 使用二维数组
  • 4 数组的基本操作
    • 4.1 遍历数组
    • 4.2 填充替换数组元素
    • 4.3 对数组进行排序
    • 4.4 复制数组
    • 4.5 数组查询
  • 5 数组排序算法
    • 5.1 冒泡排序
      • 5.1.1 基本思想
      • 5.1.2 算法实现
    • 5.2 直接选择排序
      • 5.2.1 基本思想
      • 5.2.2 算法实现
    • 5.3 反转排序
      • 5.3.1 基本思想
      • 5.3.2 算法实现

1 数组概述

数组是具有相同数据类型的一-组数据的集合。例如,球类的集合一-足球、 篮球、羽毛球等;电器集合一电视机、 洗衣机、电风扇等。在程序设计中,可以将这些集合称为数组。数组中的每个元素具有相同的数据类型。在Java中同样将数组看作是一个对象,虽然基本数据类型不是对象,但是由基本数据类型组成的数组则是对象。在程序设计中引入数组可以更有效地管理和处理数据。可根据数组的维数将数组分为一维数组、二维数组…

2 一维数组的创建与使用

2.1 创建一维数组

数组作为对象允许使用new关键字进行内存分配。在使用数组之前,必须首先定义数组变量所属的类型,一维数组的创建有两种方式。

1. 先声明,再用new运算符进行内存分配

  1. 数组元素类型 名字[];
  2. 数组元素类型[] 名字;
  3. //具体声明如下:
  4. int arr[];
  5. String str[];

声明数组后还不能立即访问它的任何元素,因为声明数组只是给出了数组名字和元素的数据类型,要想使用数组,还要为它分配内存空间,必须指明数组的长度,格式如下:

  1. 数组名字 = new 数组元素的类型[数组元素的个数];
  2. //实例代码如下:
  3. arr = new int[5];

注意:使用new关键字为数组分配内存时,整型数组中各个元素的初始值都为0

2. 声明的同时为数组分配内存 :(java)中普遍使用的方法
语法如下:

  1. 数组元素的类型 数组名 = new 数组元素的类型[数组元素个数]
  2. //实例代码如下:
  3. int month[] = new int[12];

2.2 初始化一维数组

在这里插入图片描述

2.3 使用一维数组

例2.3 创建类GetDay,在主方法中创建int型数组,并实现将各月的天数输出:

  1. public class GetDay //创建类
  2. {
  3. public static void main(String[] args) //创建主方法
  4. {
  5. int[] day = new int[]{
  6. 31,28,31,30,31,30,31,31,30,31,30,31};
  7. for(int i=0;i<12;i++)
  8. System.out.println((i+1)+"月有"+day[i]+"天");
  9. }
  10. }
  11. //输出示例
  12. /*
  13. 1月有31天
  14. 2月有28天
  15. 3月有31天
  16. 4月有30天
  17. 5月有31天
  18. 6月有30天
  19. 7月有31天
  20. 8月有31天
  21. 9月有30天
  22. 10月有31天
  23. 11月有30天
  24. 12月有31天
  25. Process finished with exit code 0
  26. */

3 二维数组的创建与使用

如果一维数组中的各个元素仍然是一个数组,那么它就是一个二维数组。

3.1 创建二维数组

1. 先声明,再用new运算符进行内存分配

  1. 数组元素类型 名字[][];
  2. 数组元素类型[][] 名字;
  3. //具体声明如下:
  4. int myarr[][];

同一维数组一样,二维数组在声明时也没有分配内存空间,同样要使用new关键字来分配内存,然后才可以进行访问每个元素,对于高维数组,有两种为数组分配内存的方式:

  1. //(1)直接为每一维分配内存空间,实例如下:
  2. a = new int[2][4];
  3. //(2)分别为每一维分配内存空间
  4. a = new int[2][];
  5. a[0] = new int[2];
  6. a[1] = new int[3];

2. 声明的同时为数组分配内存

3.2 初始化二维数组

  1. type arrayname[][] = {
  2. value1,value2...valuen};
  3. //type:数组数据类型
  4. //arrayneme:数组名称,一个合法的标识符
  5. //value:数组中各元素的值
  6. //初始化实例如下:
  7. int myarr[][] = {
  8. {
  9. 12,0},{
  10. 45,10}};

3.3 使用二维数组

例3.3 创建类Matrix,在主方法中编写代码实现输出一个3行4列且所有元素都为0的矩阵:

  1. public class Matrix //创建类
  2. {
  3. public static void main(String[] args) //创建主方法
  4. {
  5. int[][] a = new int[3][4];
  6. for(int i=0;i<a.length;i++)
  7. {
  8. for(int j=0;j<a[i].length;j++)
  9. System.out.print(a[i][j]);
  10. System.out.println();
  11. }
  12. }
  13. }
  14. //输出示例
  15. /*
  16. 0000
  17. 0000
  18. 0000
  19. Process finished with exit code 0
  20. */

4 数组的基本操作

4.1 遍历数组

遍历二维数组需要使用双层for循环,通过数组的length属性可获得数组的长度
例4.1.1 创建类Trap,在主方法中编写代码,定义二维数组,将二维数组中的元素呈梯形输出:

  1. public class Trap {
  2. public static void main(String[] args) {
  3. int[][] a = new int[][]{
  4. {
  5. 1}, {
  6. 2, 3}, {
  7. 4, 5, 6}};//定义二维数组
  8. for (int i = 0; i < a.length; i++) {
  9. //循环遍历二维数组中的每个元素
  10. for (int j = 0; j < a[i].length; j++) {
  11. System.out.print(a[i][j]);//将数组中的元素输出
  12. }
  13. System.out.println();//输出空格
  14. }
  15. }
  16. }
  17. //输出示例:
  18. /*
  19. 1
  20. 23
  21. 456
  22. Process finished with exit code 0
  23. */

在遍历数组时,使用foreach语句可能会更加简单,下面的实例就是通过foreach语句遍历二维数组。
例4.1.2 创建类Tautog,在主方法中定义二维数组,使用foreach语句遍历二维数组:

  1. public class Tautog {
  2. public static void main(String[] args) {
  3. int[][] arr2 = new int[][]{
  4. {
  5. 4, 3}, {
  6. 1, 2}}; //定义二维数组
  7. System.out.println("数组中的元素是:");
  8. int i = 0;//外层循环计数器变量
  9. for (int[] x :
  10. arr2) {
  11. //外层循环变量为一维数组
  12. i++; //外层计数器递增
  13. int j = 0; //内层循环计数器
  14. for (int e :
  15. x) {
  16. // 循环遍历每一个数组元素
  17. j++; // 内层循环计数器递增
  18. if (i == arr2.length && j == x.length) {
  19. // 判断变量是二维数组中的最后一个元素
  20. System.out.print(e); //输出二维数组最后一个元素
  21. } else {
  22. //如果不是二维数组中的最后一个元素
  23. System.out.print(e + "、"); // 输出信息
  24. }
  25. }
  26. }
  27. }
  28. }
  29. //输出示例:
  30. /*
  31. 数组中的元素是:
  32. 4、3、1、2
  33. Process finished with exit code 0
  34. */

4.2 填充替换数组元素

数组中的元素定义完成后,可通过Arrays类的静态方法il()来对数组中的元素进行替换。该方法通过各种重载形式可完成任意类型的数组元素的替换。fll()方法有两种参数类型。下面以int型数组为例介绍ill()方法的使用方法。

  1. 1fill(int[] a,int value)
  2. //该方法可将指定的int值分配给int型数组的每个元素,语法如下:
  3. fill(int[] a,int value);
  4. //a:要进行元素替换的数组
  5. //value:要存储数组中所有元素的值

例4.2.1 创建类Swap,在主方法中创建一维数组,并实现通过fill()方法填充数组元素,最后将数组中各个元素输出。

  1. import java.util.Arrays; // 导入java.util.Arrays类
  2. public class Swap {
  3. public static void main(String[] args) {
  4. int[] arr = new int[5]; // 创建int型数组
  5. Arrays.fill(arr, 8); // 使用同一个值对数组进行填充
  6. for (int i = 0; i < arr.length; i++) {
  7. // 循环遍历数组中的元素
  8. System.out.println("第" + i + "个元素是" + arr[i]);
  9. }
  10. }
  11. }
  12. //输出示例:
  13. /*
  14. 第0个元素是8
  15. 第1个元素是8
  16. 第2个元素是8
  17. 第3个元素是8
  18. 第4个元素是8
  19. Process finished with exit code 0
  20. */
  21. (2)fill(int[] a,int fromIndex,int toIndex,int value)
  22. //该方法将指定的int值分配给int型数组指定范围中的每个元素。填充的范围从索引fromIndex(包括)到索引toIndex(不包括),如果fromIndex==toIndex,则填充范围为空,语法如下:
  23. fill(int[] a,int fromIndex,int toIndex,int value);
  24. //a:要进行填充的数组
  25. //fromIndex:要使用指定值填充的第一个元素的索引(包括)
  26. //toIndex:要使用指定值填充的最后一个元素的索引(不包括)
  27. //value:要存储在数组所有元素的值

注意:如果指定的索引位置大于或者等于要进行填充的数组长度,则会报出ArrayIndexOutOf-BoundsException(数组越界异常) 异常。

例4.2.2 创建类Displace,创建一维数组,并通过fill()方法替换数组元素,最后将数组中的各个元素输出。

  1. import java.util.Arrays; // 导入java.util.Arrays类
  2. public class Displace {
  3. public static void main(String[] args) {
  4. int[] arr = new int[]{
  5. 45,12,2,10}; // 创建int型数组
  6. Arrays.fill(arr,1,2,8); // 使用同一个值对数组进行填充
  7. for (int i = 0; i < arr.length; i++) {
  8. // 循环遍历数组中的元素
  9. System.out.println("第" + i + "个元素是" + arr[i]);
  10. }
  11. }
  12. }
  13. //输出示例:
  14. /*
  15. 第0个元素是45
  16. 第1个元素是8
  17. 第2个元素是2
  18. 第3个元素是10
  19. Process finished with exit code 0
  20. */

4.3 对数组进行排序

通过Arrays类的静态sort()方法可以实现对数组的排序。sort()方法提供了多种重载形式,可对任意类型的数组进行升序排序。语法如下:

  1. Arrays.sort(object)
  2. //object:指进行排序的数组名称

例4.3 创建类Taxis,在主方法中创建一维数组,将数组排序后输出。

  1. import java.util.Arrays; // 导入java.util.Arrays类
  2. public class Taxis {
  3. public static void main(String[] args) {
  4. int[] arr = new int[]{
  5. 23,42,12,8}; // 创建int型数组
  6. Arrays.sort(arr); // 将数组进行排序
  7. for (int i = 0; i < arr.length; i++) {
  8. // 循环遍历数组中的元素
  9. System.out.println(arr[i]);
  10. }
  11. }
  12. }
  13. //输出示例:
  14. /*
  15. 8
  16. 12
  17. 23
  18. 42
  19. Process finished with exit code 0
  20. */

4.4 复制数组

Arrarys类的copyOf()方法与copyOfRange()方法可实现对数组的复制。copyOf()方法是复制数组至指定长度,copyOfRange()方法则将指定数组的指定长度复制到一个新数组中。

  1. 1copyOf()方法,语法如下:
  2. copyOf(arr,int newlength);
  3. //arr:要进行复制的数组
  4. //newlenghth:int型常量,指复制后的新数组长度。如果新数组长度大于数组arr的长度,则用0填充(整型0填充,char型null填充),如果复制后的数组长度小于数组arr的长度,则会从数组arr的第一个元素开始截取到满足新数组长度为止

例4.4.1 创建类Cope,在主方法中创建一维数组,实现将此数组复制得到一个长度为5的新数组,并将新数组输出:

  1. import java.util.Arrays; // 导入java.util.Arrays类
  2. public class Cope {
  3. public static void main(String[] args) {
  4. int[] arr = new int[]{
  5. 23, 42, 12}; // 创建int型数组
  6. int[] newarr = Arrays.copyOf(arr, 5); // 复制数组arr
  7. for (int i = 0; i < newarr.length; i++) {
  8. // 循环遍历数组中的元素
  9. System.out.println(newarr[i]);
  10. }
  11. }
  12. }
  13. //输出示例:
  14. /*
  15. 23
  16. 42
  17. 12
  18. 0
  19. 0
  20. Process finished with exit code 0
  21. */
  22. 2copyOfRange()方法,语法如下:
  23. copyOfRange(arr,int fromIndex,int toIndex);
  24. //arr:要进行复制的数组对象
  25. //fromIndex:指定开始复制数组的索引位置,必须在0-整个数组长度之间,新数组包括fromIndex元素
  26. //toIndex:要复制范围的最后索引位置,可以大于数组arr长度,新数组不包括toIndex元素

例4.4.2 创建类Repeat,在主方法中创建一维数组,并将数组中索引位置是0-3的元素复制到新数组中,最后将新数组输出:

  1. import java.util.Arrays; // 导入java.util.Arrays类
  2. public class Repeat {
  3. public static void main(String[] args) {
  4. int[] arr = new int[]{
  5. 23, 42, 12, 84, 10}; // 创建int型数组
  6. int[] newarr = Arrays.copyOfRange(arr, 0, 3); // 复制数组arr
  7. for (int i = 0; i < newarr.length; i++) {
  8. // 循环遍历数组中的元素
  9. System.out.println(newarr[i]);
  10. }
  11. }
  12. }
  13. //输出示例:
  14. /*
  15. 23
  16. 42
  17. 12
  18. Process finished with exit code 0
  19. */

4.5 数组查询

Arrays类的binarySearch()方法,可使用二分搜索法来搜索指定数组,以获得指定对象。该方法返回要搜索元素的索引值。binarySearch()方法提供 了多种重载形式,用于满足各种类型数组的查找需要。binarySearch()方法有两种参数类型。

  1. (1) binarySearch(Object[] a, Object key)
  2. //a:要搜索的数组
  3. //key:要搜索的值

例5.1.1 创建Reference类,在主方法中创建一维数组arr,实现查找元素4在数组中的索引:

  1. import java.util.Arrays; // 导入java.util.Arrays类
  2. public class Reference {
  3. public static void main(String[] args) {
  4. int[] arr = new int[]{
  5. 1, 8, 9, 4, 5}; // 创建int型数组
  6. Arrays.sort(arr);//对数组进行排序
  7. int index=Arrays.binarySearch(arr,4);
  8. System.out.println("4的索引位置是:"+index);
  9. }
  10. }
  11. //输出示例:
  12. /*cd
  13. 4的索引位置是:1
  14. Process finished with exit code 0
  15. */
  16. 2binarySearch(Object[] a,int fromlndex , int tolndex,Object key)
  17. //该方法在指定的范围内检索某一元素
  18. //a:要进行检索的数组
  19. //fromIndex:指定范围的开始索引处(包含)
  20. //toIndex:指定范围处的索引(不包含)
  21. //key:要搜索的元素

例5.1.2 创建Rakel类,在主方法创建String数组,实现查找元素“cd”在指定范围的数组str中的索引位置:

  1. import java.util.Arrays; // 导入java.util.Arrays类
  2. public class Rakel {
  3. public static void main(String[] args) {
  4. String[] str = new String[]{
  5. "ab","cd","ef","yz"};//定义String型数组
  6. Arrays.sort(str); // 将数组进行排序
  7. int index = Arrays.binarySearch(str,0,2,"cd");
  8. System.out.println("cd的索引位置是:"+index);
  9. }
  10. }
  11. //输出示例:
  12. /*
  13. cd的索引位置是:1
  14. Process finished with exit code 0
  15. */

5 数组排序算法

5.1 冒泡排序

冒泡排序是最常用的数组排序算法之一,它排序数组元素的过程总是小数往前放,大数往后放,类似水中气泡往上升的动作,所以称作冒泡排序。

5.1.1 基本思想

是对比相邻的元素值,如果满足条件就交换元素值,把较小的元素移动到数组前面,把较大的元素移动到数组后面,这样较小的元素就像气泡一样从底部上升到顶部。

5.1.2 算法实现

例5.1.2 创建BubbleSort类,实现冒泡排序的一个演示,其中排序使用的是正排序:

  1. /**
  2. * 冒泡排序算法实例
  3. *
  4. * @author Codeplus
  5. */
  6. public class BubbleSort {
  7. public static void main(String[] args) {
  8. //创建一个数组,这个数组元素是乱序的
  9. int[] array = new int[]{
  10. 64, 4, 24, 1, 3, 15};
  11. //创建冒泡排序类的对象
  12. BubbleSort sorter = new BubbleSort();
  13. //调用排序方法将数组进行排序
  14. sorter.sort(array);
  15. }
  16. /**
  17. * 冒泡排序
  18. *
  19. * @param array 要排序的数组
  20. */
  21. public void sort(int[] array) {
  22. i
  23. for (int i = 1; i < array.length; i++) {
  24. //比较相邻的两个元素,较大的数往后冒泡
  25. for (int j = 0; j < array.length - i; j++) {
  26. if (array[j] > array[j + 1]) {
  27. int temp = array[j];//把第一个元素的值保存在临时变量里
  28. array[j] = array[j + 1];//把第二个元素值保存到第一个元素单元里
  29. array[j + 1] = temp;//把临时变量保存到第二个元素中
  30. }
  31. }
  32. }
  33. showArray(array); //输出冒泡排序后的数组元素
  34. }
  35. /**
  36. * 显示数组中的所有元素
  37. *
  38. * @param array 要显示的数组
  39. */
  40. public void showArray(int[] array) {
  41. for (int i :
  42. array) {
  43. //遍历数组
  44. System.out.print(" >" + i); //输出每个数组元素值
  45. }
  46. System.out.println();
  47. }
  48. }
  49. //输出示例:
  50. /*
  51. >1 >3 >4 >15 >24 >64
  52. Process finished with exit code 0
  53. */

5.2 直接选择排序

5.2.1 基本思想

将指定排序位置与其他数组元素分别对比,如果满足条件就交换元素值。注意这里与冒泡排序的区别,不是交换相邻元素,而是把满足条件的元素与指定的排序位置交换,这样排序好的位置逐渐扩大,最后整个数组都成为已经排序好的格式。

5.2.2 算法实现

例5.2.2 创建SelectSort类,这个类作为直接选择排序的一个演示,其中排序选择的是正排序:

  1. /**
  2. * 直接选择排序算法实例
  3. *
  4. * @author Codeplus
  5. */
  6. public class SelectSort {
  7. public static void main(String[] args) {
  8. //创建一个数组,这个数组元素是乱序的
  9. int[] array = new int[]{
  10. 64, 4, 24, 1, 3, 15};
  11. //创建直接选择排序类的对象
  12. SelectSort sorter = new SelectSort();
  13. //调用排序方法将数组进行排序
  14. sorter.sort(array);
  15. }
  16. /**
  17. * 直接选择排序
  18. *
  19. * @param array 要排序的数组
  20. */
  21. public void sort(int[] array) {
  22. int index;
  23. for (int i = 1; i < array.length; i++) {
  24. index=0;
  25. for (int j = 1; j <= array.length - i; j++) {
  26. if (array[j] > array[index]) {
  27. index=j;
  28. }
  29. }
  30. //交换在位置array.length-i和index(最大值)上的两个数
  31. int temp = array[array.length-i];//把第一个元素的值保存在临时变量里
  32. array[array.length-i] = array[index];//把第二个元素值保存到第一个元素单元里
  33. array[index] = temp;//把临时变量保存到第二个元素中
  34. }
  35. showArray(array); //输出冒泡排序后的数组元素
  36. }
  37. /**
  38. * 显示数组中的所有元素
  39. *
  40. * @param array 要显示的数组
  41. */
  42. public void showArray(int[] array) {
  43. for (int i :
  44. array) {
  45. //遍历数组
  46. System.out.print(" >" + i); //输出每个数组元素值
  47. }
  48. System.out.println();
  49. }
  50. }
  51. //输出示例:
  52. /*
  53. >1 >3 >4 >15 >24 >64
  54. Process finished with exit code 0
  55. */

5.3 反转排序

5.3.1 基本思想

把数组最后一个元素与第一个元素交换,倒数第二个元素与第二个元素替换,以此类推,直到把所有数组元素反转替换

5.3.2 算法实现

例5.3.2 创建ReverseSort类,实现反转排序演示:

  1. /**
  2. * 反转排序算法实例
  3. *
  4. * @author Codeplus
  5. */
  6. public class ReverseSort {
  7. public static void main(String[] args) {
  8. // 创建一个数组
  9. int[] array = {
  10. 10, 20, 30, 40, 50, 60};
  11. // 创建反转排序类的对象
  12. ReverseSort sorter = new ReverseSort();
  13. // 调用排序对象的方法将数组反转
  14. sorter.sort(array);
  15. }
  16. /**
  17. * 直接选择排序法
  18. *
  19. * @param array 要排序的数组
  20. */
  21. public void sort(int[] array) {
  22. System.out.println("数组原有内容:");
  23. showArray(array);// 输出排序前的数组值
  24. int temp;
  25. int len = array.length;
  26. for (int i = 0; i < len / 2; i++) {
  27. temp = array[i];
  28. array[i] = array[len - 1 - i];
  29. array[len - 1 - i] = temp;
  30. }
  31. System.out.println("数组反转后内容:");
  32. showArray(array);// 输出排序后的数组值
  33. }
  34. /**
  35. * 显示数组所有元素
  36. *
  37. * @param array 要显示的数组
  38. */
  39. public void showArray(int[] array) {
  40. for (int i : array) {
  41. // foreach格式遍历数组
  42. System.out.print("\t" + i);// 输出每个数组元素值
  43. }
  44. System.out.println();
  45. }
  46. }
  47. //输出示例:
  48. /*
  49. 数组原有内容:
  50. 10 20 30 40 50 60
  51. 数组反转后内容:
  52. 60 50 40 30 20 10
  53. Process finished with exit code 0
  54. */

发表评论

表情:
评论列表 (有 0 条评论,291人围观)

还没有评论,来说两句吧...

相关阅读