数组

1. 数组的概念

在内存中一块 连续 的空间,存储数据类型相同的内容,长度是固定的。

2. 数组的定义

1.先声明、再分配空间: 数据类型[] 数组名; 数组名 = new 数据类型[长度]; 2.声明并分配空间: 数据类型[] 数组名 = new 数据类型[长度]; 3.声明并赋值(繁): 数据类型[] 数组名 = new 数据类型[]{value1,value2,value3,…}; 4.声明并赋值(简): 数据类型[] 数组名 = {value1,value2,value3,…}; //显示初始化,注意:不可换行

  1. package com.qfedu.test1;
  2. /**
  3. * 数组定义的方式
  4. * 数组的特点:
  5. * 在内存中一块 连续 的空间,存储数据类型 相同 的内容,长度是 固定 的。
  6. * @author WHD
  7. *
  8. */
  9. public class Test1 {
  10. public static void main(String[] args) {
  11. // 方式1 先声明 再分配空间
  12. int a;
  13. a = 10;
  14. int [] arr1;
  15. arr1 = new int[5];
  16. // 方式2 连声明 带 分配空间
  17. int [] arr2 = new int[3];
  18. // 方式3 声明并且赋值 繁琐
  19. int [] arr3 = new int[] {1,3,5,7,9};
  20. // 方式4 声明并且赋值 简单
  21. int [] arr4 = {2,4,6,8,10};
  22. }
  23. }
  1. package com.qfedu.test2;
  2. /**
  3. * 各种类型数组
  4. * @author WHD
  5. *
  6. */
  7. public class Test1 {
  8. public static void main(String[] args) {
  9. byte [] arr1 = new byte[10];
  10. short [] arr2 = {1,2,3,4,5};
  11. int [] arr3 = new int[] {1,2,3,4,5};
  12. long [] arr4 = new long[10];
  13. float [] arr5 = {1,2,3,4,5};
  14. double [] arr6 = new double[10];
  15. boolean [] arr7 = new boolean[3];
  16. char [] arr8 = new char[] {'a','b','c'};
  17. String [] arr9 = {"a","b","c"};
  18. }
  19. }

3.数组的访问

数组的访问:赋值和取值 通过下标访问数组的元素 ,下标从0开始 依次加1

  1. package com.qfedu.test2;
  2. /**
  3. * 数组的访问:赋值和取值
  4. * 通过下标访问数组的元素 下标从0开始 依次加1
  5. * @author WHD
  6. *
  7. */
  8. public class Test2 {
  9. public static void main(String[] args) {
  10. int [] arr1 = new int[5];
  11. arr1[0] = 10;
  12. arr1[1] = 12;
  13. arr1[2] = 15;
  14. arr1[3] = 22;
  15. arr1[4] = 35;
  16. // arr1[5] = 66; 数组下标越界异常ArrayIndexOutOfBoundsException 不能访问超过数组下标的范围
  17. System.out.println(arr1[0]);
  18. System.out.println(arr1[1]);
  19. System.out.println(arr1[2]);
  20. System.out.println(arr1[3]);
  21. System.out.println(arr1[4]);
  22. // System.out.println(arr1[21]);数组下标越界异常ArrayIndexOutOfBoundsException
  23. }
  24. }

4. 数组的遍历

数组的遍历:逐一将数组中的元素进行访问 数组的属性:length 表示数组的长度 是一个int类型的数值 使用方式:数组名.length 在遍历数组的使用 推荐使用length作为判断条件 更准确 不会出错

  1. package com.qfedu.test3;
  2. /**
  3. * 数组的遍历:逐一将数组中的元素进行访问
  4. * 数组的属性:length 表示数组的长度 是一个int类型的数值
  5. * 使用方式:数组名.length
  6. *
  7. * 在遍历数组的使用 推荐使用length作为判断条件 更准确 不会出错
  8. * @author WHD
  9. *
  10. */
  11. public class Test1 {
  12. public static void main(String[] args) {
  13. int [] arr1 = {11,22,33,44,55,2,454,1,56,1,4,4,5,7,85,1,4,56,11,22,33,44,55,2,454,1,56,1,4,4,5,7,85,1,4,5611,22,33,44,55,2,454,1,56,1,4,4,5,7,85,1,4,5611,22,33,44,55,2,454,1,56,1,4,4,5,7,85,1,4,5611,22,33,44,55,2,454,1,56,1,4,4,5,7,85,1,4,56};
  14. System.out.println(arr1.length);
  15. for(int i = 0;i < arr1.length;i++) {
  16. System.out.println(arr1[i]);
  17. }
  18. }
  19. }

5. 数组的默认值

数组的默认值 整数:0 小数:0.0 字符:\u0000 布尔:false 其他:null

  1. package com.qfedu.test3;
  2. /**
  3. * 数组的默认值
  4. * 整数:0
  5. * 小数:0.0
  6. * 字符:\u0000
  7. * 布尔:false
  8. * 其他:null
  9. * @author WHD
  10. *
  11. */
  12. public class Test2 {
  13. public static void main(String[] args) {
  14. byte [] arr1 = new byte[3];
  15. for(int i = 0;i < arr1.length;i++) {
  16. System.out.print(arr1[i] + "\t");
  17. }
  18. System.out.println();
  19. short [] arr2 = new short[5];
  20. for (int i = 0; i < arr2.length; i++) {
  21. System.out.print(arr2[i] + "\t");
  22. }
  23. System.out.println();
  24. float [] arr3 = new float[6];
  25. for (int i = 0; i < arr3.length; i++) {
  26. System.out.print(arr3[i] + "\t");
  27. }
  28. System.out.println();
  29. char [] arr4 = new char[7];
  30. for (int i = 0; i < arr4.length; i++) {
  31. System.out.print(arr4[i] + "-");
  32. }
  33. System.out.println();
  34. boolean [] arr5 = new boolean[2];
  35. for (int i = 0; i < arr5.length; i++) {
  36. System.out.print(arr5[i] + "\t");
  37. }
  38. System.out.println();
  39. String [] arr6 = new String[5];
  40. for (int i = 0; i < arr6.length; i++) {
  41. System.out.print(arr6[i] + "\t");
  42. }
  43. }
  44. }

6. 数组的扩容

数组的扩容 实现思路: 定义一个更长的数组 将原数组中的元素逐一复制到新的数组中

  1. package com.qfedu.test4;
  2. /**
  3. * 数组的扩容
  4. * 实现思路:
  5. * 定义一个更长的数组 将原数组中的元素逐一复制到新的数组中
  6. * @author WHD
  7. *
  8. */
  9. public class Test1 {
  10. public static void main(String[] args) {
  11. int [] arr1 = new int[5];
  12. arr1[0] = 11;
  13. arr1[1] = 22;
  14. arr1[2] = 33;
  15. arr1[3] = 44;
  16. arr1[4] = 55;
  17. int arr2[] = new int[arr1.length * 2];
  18. for(int i = 0;i < arr1.length;i++) {
  19. arr2[i] = arr1[i];
  20. }
  21. for(int i = 0;i < arr2.length;i++) {
  22. System.out.print(arr2[i] + "\t");
  23. }
  24. }
  25. }

7.数组的复制

7.1 循环实现

  1. package com.qfedu.test5;
  2. /**
  3. * 复制数组
  4. * 1. 使用循环复制
  5. * @author WHD
  6. *
  7. */
  8. public class Test1 {
  9. public static void main(String[] args) {
  10. int [] arr1 = {11,22,33,44,55};
  11. int [] arr2 = new int[arr1.length * 2];
  12. for(int i =0;i < arr1.length;i++) {
  13. arr2[i] = arr1[i];
  14. }
  15. }
  16. }

7.2 System.arraycopy方法

  1. package com.qfedu.test5;
  2. /**
  3. * 2. System.arraycopy(原数组,原数组起始,新数组,新数组起始,长度);
  4. * @author WHD
  5. *
  6. */
  7. public class Test2 {
  8. public static void main(String[] args) {
  9. int [] arr1 = {11,22,33,44,55};
  10. int [] arr2 = new int[arr1.length * 2];
  11. // System.arraycopy(arr1, 0, arr2, 0, 5);
  12. // System.arraycopy(arr1, 1, arr2, 2, 3);
  13. System.arraycopy(arr1, 3, arr2, 5, 2);
  14. for (int i = 0; i < arr2.length; i++) {
  15. System.out.print(arr2[i] + "\t");
  16. }
  17. System.out.println();
  18. }
  19. }

7.3 Arrays.copyOf方法

  1. package com.qfedu.test5;
  2. import java.util.Arrays;
  3. /**
  4. * 3. Arrays.copyOf(原数组,新长度)
  5. * @author WHD
  6. *
  7. */
  8. public class Test3 {
  9. public static void main(String[] args) {
  10. int [] ar1 = {11,22,33,44,55};
  11. int arr2[] = Arrays.copyOf(arr1, 10); // 长度超过原数组将以默认值填充
  12. for (int i = 0; i < arr2.length; i++) {
  13. System.out.print(arr2[i] + "\t");
  14. }
  15. System.out.println();
  16. }
  17. }

8.值传递和引用传递(面试题)

值传递和引用传递的区别? 基本数据类型参数传递传递的是值本身 不会影响原来的变量 引用数据类型参数传递传递的是地址 会影响原来的变量 String类型属于特殊的引用数据类型 作为参数传递原来的不会改变

  1. package com.qfedu.test7;
  2. /**
  3. * 值传递
  4. * 基本数据类型参数传递 传递的都是值 不会影响原来的变量的值
  5. * @author WHD
  6. *
  7. */
  8. public class Test1 {
  9. public static void main(String[] args) {
  10. int a = 10;
  11. m1(a);
  12. System.out.println("实参变量a的取值为" + a);
  13. }
  14. public static void m1(int num) {
  15. num+=10;
  16. System.out.println("m1方法中num的取值为" + num);
  17. }
  18. }
  1. package com.qfedu.test7;
  2. /**
  3. * 引用传递
  4. * 引用传递 引用数据类型传递 传递的是地址 所以会影响所有的变量
  5. * @author WHD
  6. *
  7. */
  8. public class Test2 {
  9. public static void main(String[] args) {
  10. int [] a = {1,3,5,7,9};
  11. System.out.println(a);
  12. m1(a);
  13. for (int i = 0; i < a.length; i++) {
  14. System.out.print(a[i] + "\t");
  15. }
  16. }
  17. public static void m1(int [] nums) {
  18. System.out.println(nums);
  19. for (int i = 0; i < nums.length; i++) {
  20. nums[i] ++;
  21. }
  22. }
  23. }
  1. package com.qfedu.test7;
  2. /**
  3. * String类型是特殊的引用数据类型 所以作为参数传递 不会改变原来的变量
  4. * @author WHD
  5. *
  6. */
  7. public class Test3 {
  8. public static void main(String[] args) {
  9. String a = "hello";
  10. m1(a);
  11. System.out.println(a);
  12. }
  13. public static void m1(String str) {
  14. str += "abc";
  15. }
  16. }

9.数组参数和数组返回值

数组类型的参数 使用方式与之前用法一样

  1. package com.qfedu.test6;
  2. /**
  3. * 数组类型的参数 使用方式与之前用法一样
  4. * @author WHD
  5. *
  6. */
  7. public class Test3 {
  8. public static void main(String[] args) {
  9. String [] strs = {"a","b","c"};
  10. m1(strs);
  11. }
  12. public static void m1(String [] strs) {
  13. for (int i = 0; i < strs.length; i++) {
  14. System.out.println(strs[i]);
  15. }
  16. }
  17. }
  1. package com.qfedu.test6;
  2. import java.util.Scanner;
  3. /**
  4. * 数组类型的返回值
  5. * 编写一个方法让用户输入个人的 三门成绩 并且保存到数组中返回
  6. * @author WHD
  7. *
  8. */
  9. public class Test4 {
  10. public static void main(String[] args) {
  11. double [] scores = getScore();
  12. for (int i = 0; i < scores.length; i++) {
  13. System.out.print(scores[i] + "\t");
  14. }
  15. }
  16. public static double[] getScore() {
  17. Scanner input = new Scanner(System.in);
  18. double [] scores = new double[3];
  19. for(int i = 0;i < scores.length;i++) {
  20. System.out.println("请输入第"+(i + 1)+"门成绩");
  21. scores[i] = input.nextDouble();
  22. }
  23. return scores;
  24. }
  25. }