JAVASE基础模块六(数组)

JAVASE基础模块六

数组

  • 数组就是一个容器 可以存储多个数据类型一致的元素
  • Java中的数组 一旦定义,长度就固定了
  • 数组即可以存储基本数据类型(四类八种),也可以存储引用数据类型
  • 引用数据类型:数组,类,接口 简单点,也就是说,new出来的数据就是引用数据类型

数组的异常

  • 数组引索越界异常 ArrayIndexOutOfBoundsException

    public class ShuZUTest {
        public static void main(String[] args) {
            double[] arr= {1,2,3,4,5,6};
            System.out.println(arr[6]);
    
        }
    }
    
    运行结果:
    Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 6 out of bounds for length 6
    	at com.westos.ShuZu.ShuZUTest.main(ShuZUTest.java:6)
    
    Process finished with exit code 1
    
  • 数组空指针异常 NullPointerException

    public class ShuZUTest {
        public static void main(String[] args) {
            double[] arr=null;
            System.out.println(arr[6]);
    
        }
    }
    
    运行结果:
    Exception in thread "main" java.lang.NullPointerException
    	at com.westos.ShuZu.ShuZUTest.main(ShuZUTest.java:6)
    
    Process finished with exit code 1
    
  • 数组的查表法

    public class ShuZUTest {
        public static void main(String[] args) {
            String[] arr= {"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
            System.out.println("请输入1-7的数字");
            Scanner x=new Scanner(System.in);
            int z = x.nextInt();
            System.out.println(arr[z-1]);
        }
    }
    
    运行结果:
    请输入1-7的数字
    7
    星期日
    
    Process finished with exit code 0
    
  • 数组元素的初次位置查询

    public class ShuZUTest {
        public static void main(String[] args) {
            double[] arr={1.1,2.1,3.1,4.1,5.1,6.1,7.1,8.1,9.1};
            System.out.println("请输入需要查找的元素");
            Scanner x=new Scanner(System.in);
            double z = x.nextDouble();
            int m=0;
            for (int i = 0; i < arr.length; i++) {
                if(arr[i]==z){m=i;
                break;}
            }
            System.out.println("元素"+z+"在数组中第一次出现在:"+m+"号位置");
        }
    }
    
    运行结果:
    请输入需要查找的元素
    5.1
    元素5.1在数组中第一次出现在:4号位置
    
    Process finished with exit code 0
    

二维数组+

  • 定义格式

    public class ErWeiShuZu {
        public static void main(String[] args) {
            int[][] arr=new int[3][3];
            System.out.println(arr[2][2]);
            //不推荐使用的定义格式
            int arr1[][]=new int[3][3];
            int[] arr2[]=new int[3][3];
            int x,y;//定义了两个变量
            int[] x1,y1[];//定义了x1一维数组 与 y1二维数组
    
        }
    }
    
    运行结果:
    0
    
    Process finished with exit code 0
    
  • 二维数组的遍历与实例

    public class ErWeiShuZu {
        public static void main(String[] args) {
            double[][] arr = {{22, 66, 44}, {77, 33, 88}, {25, 45, 65}, {11, 66, 99}};
            see(arr);
            yue(arr);
            he(arr);
        }
    
        public static void yue(double[][] arr) {
            System.out.println("请输入要查询月份的销售额(1-12月)");
            Scanner x = new Scanner(System.in);
            int d = x.nextInt();
            if (d >= 1 && d <= 3) {
                System.out.println("您要查询的结果:" + d + "月份的销售额为" + arr[1][d - 1] + "万元")
            } else if (d >= 4 && d <= 6) {
                System.out.println("您要查询的结果:" + d + "月份的销售额为" + arr[2][d - 4] + "万元");
            } else if (d >= 7 && d <= 9) {
                System.out.println("您要查询的结果:" + d + "月份的销售额为" + arr[2][d - 7] + "万元");
            } else if (d >= 10 && d <= 12) {
                System.out.println("您要查询的结果:" + d + "月份的销售额为" + arr[2][d - 10] + "万元");
            } else {
                System.out.println("输入的查询月份有误");
            }
        }
    
        public static void he(double[][] arr) {
            System.out.println("请输入你要进行的选项(1-5)");
            System.out.println("1.查询第一季度的销售总额为");
            System.out.println("2.查询第二季度的销售总额为");
            System.out.println("3.查询第三季度的销售总额为");
            System.out.println("4.查询第四季度的销售总额为");
            System.out.println("5.查询一年的销售总额为");
            Scanner xx = new Scanner(System.in);
            int dd = xx.nextInt();
            int sum = 0;
            if (dd == 1) {
                for (int i = 0; i < arr[0].length; i++) {
                    sum += arr[1][i];
                }
                System.out.println("结果:" + dd + "季度的销售总额为" + sum + "万元");
            } else if (dd == 2) {
                for (int i = 0; i < arr[1].length; i++) {
                    sum += arr[1][i];
                }
                System.out.println("结果:" + dd + "季度的销售总额为" + sum + "万元");
            } else if (dd == 3) {
                for (int i = 0; i < arr[2].length; i++) {
                    sum += arr[1][i];
                }
                System.out.println("结果:" + dd + "季度的销售总额为" + sum + "万元");
            } else if (dd == 4) {
                for (int i = 0; i < arr[3].length; i++) {
                    sum += arr[1][i];
                }
                System.out.println("结果:" + dd + "季度的销售总额为" + sum + "万元");
            } else if (dd == 5) {
                for (int i = 0; i < arr.length; i++) {
                    for (int i1 = 0; i1 < arr[i].length; i1++) {
                        sum += arr[i][i1];
                    }
    
                }
                System.out.println("结果:一年的销售总额为" + sum + "万元");
            } else {
                System.out.println("输入的查询季度有误");
            }
        }
    
        public static void see(double[][] arr) {
            System.out.println("某公司按照季度和月份统计的数据如下:单位(万元)");
            System.out.print("1.第一季度各月份销售额为:");
            for (int i = 0; i < arr[0].length; i++) {
                System.out.print(arr[0][i] + ",");
            }
            System.out.println();
            System.out.print("2.第二季度各月份销售额为:");
            for (int i = 0; i < arr[1].length; i++) {
                System.out.print(arr[1][i] + ",");
            }
            System.out.println();
            System.out.print("3.第三季度各月份销售额为:");
            for (int i = 0; i < arr[2].length; i++) {
                System.out.print(arr[2][i] + ",");
            }
            System.out.println();
            System.out.print("4.第四季度各月份销售额为:");
            for (int i = 0; i < arr[3].length; i++) {
                System.out.print(arr[3][i] + ",");
            }
            System.out.println();
        }
    }
    
    运行结果:
    某公司按照季度和月份统计的数据如下:单位(万元)
    1.第一季度各月份销售额为:22.0,66.0,44.0,
    2.第二季度各月份销售额为:77.0,33.0,88.0,
    3.第三季度各月份销售额为:25.0,45.0,65.0,
    4.第四季度各月份销售额为:11.0,66.0,99.0,
    请输入要查询月份的销售额(1-12月)
    7
    您要查询的结果:7月份的销售额为25.0万元
    请输入你要进行的选项(1-5)
    1.查询第一季度的销售总额为
    2.查询第二季度的销售总额为
    3.查询第三季度的销售总额为
    4.查询第四季度的销售总额为
    5.查询一年的销售总额为
    5
    结果:一年的销售总额为641万元
    
    Process finished with exit code 0
    
    1. 包含了二维数组的输出 遍历 查询

杨辉三角的实现与输出

  • 二维数组来实现
    1. 先进行二维数组的定义 保持行数与列数一致
    2. 先设置每行元素的首尾元素 设置为1
    3. 从二维数组的第三行与第二列开始设置数组中间元素 行0-2 列0-1
    4. 打印杨辉三角 双层for循环实现
    5. 外层控制行数 里层控制列数 里层最大值为外层当前数字
public class xxx {
    public static void main(String[] args) {
        Scanner xx = new Scanner(System.in);
        System.out.println("请输入行数");
        //行数列数一致
        int n = xx.nextInt();
        int[][] arr = new int[n][n];
        for (int i = 0; i < arr.length; i++) {
            arr[i][0] = 1;  //1.首尾元素为1
            arr[i][i] = 1;}
        for (int i = 2; i < arr.length; i++) {
            for (int j = 1; j <= i; j++) {//第三行开始的中间元素
                arr[i][j] = arr[i - 1][j - 1] + arr[i - 1][j]; }}
        for (int i = 0; i < arr.length; i++) {
            for (int i1 = 0; i1 <= i; i1++) {打印杨辉三角
                System.out.print(arr[i][i1] + "\t\t");}
            System.out.println();}}}
运行结果:
请输入行数
9
1		
1		1		
1		2		1		
1		3		3		1		
1		4		6		4		1		
1		5		10		10		5		1		
1		6		15		20		15		6		1		
1		7		21		35		35		21		7		1		
1		8		28		56		70		56		28		8		1		
Process finished with exit code 0

递归

  • 概念 JAVA中的递归是指的是 在方法中调用方法本身的这种现象

  • 在编写递归时 注意

    1. 递归要有出口 不然就是死递归会造成栈溢出

    2. 递归次数不宜过多 过多也会栈溢出

      StackOverflowError 栈溢出
      
      public class DiGui {
          public static void main(String[] args) {
              Scanner xx = new Scanner(System.in);
              System.out.println("请输入阶乘数");
              int n = xx.nextInt();
              System.out.println(jieChen(n));}
          private static int jieChen(int x) {
              if (x == 1) {
                  return 1;
              } else {
                  return x * jieChen(x - 1); }}}
      
      运行结果:
      请输入阶乘数
      99999
      Exception in thread "main" java.lang.StackOverflowError
      Process finished with exit code 1
      
  • 体现的就是拆分合并的思想

  • 生活中的递归

    1. 从前有座山山上有座庙 庙里有个老和尚 老和尚给小和尚讲故事 从前有座山…
  • 阶乘举例

    public class DiGui {
        public static void main(String[] args) {
            Scanner xx = new Scanner(System.in);
            System.out.println("请输入阶乘数");
            int n = xx.nextInt();
            System.out.println(jieChen(n));}
        private static int jieChen(int x) {
            if (x == 1) {
                return 1;
            } else {
                return x * jieChen(x - 1); }}}
    
    运行结果:
    请输入阶乘数
    5
    120
    Process finished with exit code 0
    
  • 不死神兔问题

    1. 不死神兔,有一对兔子,从出生后第三个月每个月都生一对小兔子然后一直生

      public class BuSiShenTu {
          public static void main(String[] args) {
              Scanner xx = new Scanner(System.in);
              System.out.println("请输入月份");
              int n = xx.nextInt();
              System.out.println(buSiShenTu(n)); }
          public static int buSiShenTu(int yue) {
              int[] m = new int[yue];
              if (yue == 1 || yue == 2) {
                  return 1;
              } else {
                  return buSiShenTu(yue - 1) + buSiShenTu(yue - 2);}}}
      
      运行结果:
      请输入月份
      6
      8
      Process finished with exit code 0
      

参数的传递

  • 调用方法时 参数传递 基本类型数据传递与引用类型数据传递

  • 基本类型数据传递 传到方法的是参数的值 对于没有返回值的方法 方法内参数发生变化 不会将这种变化返回到主函数中 仅仅是在方法内部发生变化 也就是所谓的形参的改变不影响实参的数据值

  • 引用类型数据传递 例如数组 传到方法内的并不是数组的元素值 JAVA中方法内传入的是数组元素的地址值 而非数组元素值 因此 如果在方法中如果发生数组元素数值的变化 就会更新到元素所在的地址值 因此 引用类型数据传递时 无返回值的方法也会改变数组内的元素值

    public class Sd {
        public static void main(String[] args) {
            int a=17;
            int b=13;
            System.out.println("3a:" + a + ",3b:" + b);
            x(a,b);
            System.out.println("4a:" + a + ",4b:" + b);
            int[] a1={1,1,1,1,1,1};
            x(a1);
            System.out.println(a1[2]);}
        public static void x(int a, int b) {
            System.out.println("1a:" + a + ",1b:" + b);
            a = 38 * a;
            b = a * 10;
            System.out.println("2a:" + a + ",2b:" + b);}
        public static void x(int[] a) {
            a[2] = 3 * a[1];}}
    
    
    运行结果:
    3a:17,3b:13
    1a:17,1b:13
    2a:646,2b:6460
    4a:17,4b:13
    3
    Process finished with exit code 0
    

猜你喜欢

转载自blog.csdn.net/cx9977/article/details/107443525