一维和二维数组,递归

一维数组

  1. 定义:数组是存储同一种数据类型多个元素的集合,也可以看成一个容器。

  2. 格式:
    格式1: 数据类型[] 数组名;
    格式2: 数据类型 数组名[];

  3. 初始化数组:数组必须初始化才能使用

  4. 初始化:
    动态初始化:只给长度,有系统给初始化值
    静态初始化:给初始化值,由系统决定长度
    两种方式只能使用一种

  5. 动态初始化格式:
    数据类型[] 数组名 = new 数据类型[数组长度];
    数组长度可以用arr.length表示,所以数组的最后一个索引为:arr.length-1.

  6. 静态初始化格式:
    格式:数据类型[] 数组名 = new 数据类型[]{元素1,元素2,…};
    简化格式:数据类型[] 数组名 = {元素1,元素2,…};

内存分配以及栈和堆的区别

  1. 栈内存:存放的是局部变量。
    局部变量:在方法声明上(形参)或在方法中。

  2. 堆内存:存放的是成员变量(后面说)就是new出来的东西。
    每一个new出来的都会分配一个地址值。
    每一个变量都有一个默认的值。
    byte,short,int,long ------- 0
    float,double ----------- 0.0
    char ---------- --’\u0000’
    boolean – false
    引用数据类型 ------- null
    使用完毕之后就等待垃圾回收器记性回收。

  3. 方法区(面向对象讲解)

  4. 本地方法区:(和系统相关)

  5. 寄存器:(CPU使用)

数组常见的异常问题

  1. ArrayIndexOutOfBoundsException:数组索引越界异常
  2. NullPointerException:空指针异常

一维数组遍历以及其他案例

例1:一维数组的遍历

public class Demo {
    public static void main(String[] args) {
        int[] arr={1,2,3,4,5};
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+"\t");
        }
    }
}

例2:一维数组的反向遍历

public class Demo2 {
    public static void main(String[] args) {
        int[] arr={1,2,3,4,5};
        for (int i =arr.length-1; i >=0; i--) {
            System.out.print(arr[i]+"\t");
        }
    }
}

例3:获取数组中的最大值或者最小值

public class Demo3 {
    public static void main(String[] args) {
        int[] arr={1,2,3,4,5};
        int max=arr[0];
        for (int i = 1; i < arr.length; i++) {
            if (arr[i]>max){
                max=arr[i];
            }
        }
        System.out.println(max);
    }
}

例4:数组中的元素对调

public class Demo4 {
    public static void main(String[] args) {
        int[] arr={1,2,3,4,5};
        for (int i=0,j=arr.length-1;i<j;i++,j--){
            int t=arr[i];
            arr[i]=arr[j];
            arr[j]=t;
        }
        for (int i = 0; i < arr.length; i++) {
            System.out.print(arr[i]+"\t");
        }
    }
}

例5:数组查表法(根据键盘录入索引,查找对应星期)

public class Demo5 {
    public static void main(String[] args) {
        String[] arr={"星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个索引 ,1---------7");
        int i = sc.nextInt();
        String weeks=add(i,arr);
        System.out.println(weeks);
    }
    public static String add(int i ,String[] arr){
        String weeks;
        if (i>=1&&i<=7){
           weeks=arr[i-1];
        }else
            weeks="不合理";
        return weeks;
    }
}

例6:查找指定元素第一次在数组中出现的索引

public class Demo6 {
    public static void main(String[] args) {
        String[] arr={"星期一","星期二","星期三","星期四","星期五","星期六","星期日","星期一","星期二","星期三","星期四","星期五","星期六","星期日"};
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个星期");
        String s = sc.nextLine();
        int index=add(s,arr);
        System.out.println(index);

    }
    public static int  add(String s, String[] arr){
        for (int j = 0; j < arr.length; j++) {
            if (s.equals(arr[j])){
            }
            return j;
        }
        return -1;
    }
}

二维数组

定义:二维数组就是每一个元素为一维数组的数组。

  1. 格式:数据类型[][] 变量名 = new 数据类型[m][n];
    其中m表示二维数组的长度,必须写上。
    n表示每个一维数组的长度,可写可不写。
    当n不写时,没有直接给出一维数组的元素个数,可以动态给出。
    例:
    int[][] arr = new int[3][];
    arr[0] = new int[2];
    arr[1] = new int[3];
    arr[2] = new int[1];

  2. 简写格式:数据类型[][] 变量名 = {{元素…},{元素…},{元素…}};

  3. int[] x,y[];定义了两个数组,一个是一维数组x,一个是二维数组y

二维数组的一些案例

例1:二维数组的遍历

public class Demo7 {
    public static void main(String[] args) {
        int[][] arr={{1,2,3},{4,5,6},{7,8,9}};
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr.length; j++) {
                System.out.print(arr[i][j]+"\t");
            }
        }
    }
}

例2:二维数组求和

public class Demo8 {
    public static void main(String[] args) {
       int[][] arr=new int[4][3];
       arr[0]=new int[]{22,66,44};
       arr[1]=new int[]{77,33,88};
       arr[2]=new int[]{25,45,65};
       arr[3]=new int[]{11,66,99};
       int sum=0;
        for (int i = 0; i < arr.length; i++) {
            for (int j = 0; j < arr[i].length; j++) {
                sum+=arr[i][j];
            }
        }
        System.out.println(sum);
    }
}

例3:打印杨辉三角形

public class Demo9 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("请输入一个行数");
        int n= sc.nextInt();
        int[][] arr=new int[n][n];
        for (int j = 0; j < arr.length; j++) {
            arr[j][0]=1;
            arr[j][j]=1;
        }
        for (int j =2; j < arr.length; j++) {
            for (int t = 1; t <=j; t++) {
                arr[j][t]=arr[j-1][t-1]+arr[j-1][t];
            }
        }
        for (int j = 0; j < arr.length; j++) {
            for (int t = 0; t <=j; t++) {
                System.out.print(arr[j][t]+"\t");
            }
            System.out.println();
        }
    }
}

Java中的参数传递问题

public class MyTest {
    public static void main(String[] args) {
        //定义两个变量
        int a = 10;
        int b = 20;
        //输出两个变量的值
        System.out.println("a: " + a + ",b: " + b); //1 a 10  b 20
        //调用方法
        change(a, b);
        //再次输出变量的值
        System.out.println("a: " + a + ",b: " + b); //4.  10 20
        //定义了一个数组
        int[] arr = {1, 2, 3, 4, 5};
        //调用方法
        change(arr);
        //输出数组中第二个元素的值
        System.out.println(arr[1]); //5.4
        //总结:基本数据类型,作为参数传递,形参的改变,不影响实参
        // 引用数据类型,作为参数传递,形参的改变,会影响到实参
    }

    public static void change(int a, int b) {
        System.out.println("a: " + a + ",b: " + b); // 2. 10 20
        a = b;
        b = a + b;
        System.out.println("a: " + a + ",b: " + b); //3. 20 40
    }

    public static void change(int[] arr) {
        for (int x = 0; x < arr.length; x++) {
            if (arr[x] % 2 == 0) {
                arr[x] *= 2;
            }
        }
    }
}

由上个代码得知:
基本数据类型,作为参数传递时,形参的改变不会影响实参的值。
引用数据类型,作为参数传递时,形参的改变会影响实参的值。
如下图所示:

参数传递

递归

  1. 概述:方法定义中调用方法本身的现象。

  2. 注意事项:
    必须要有出口,否则就是死递归
    递归次数不能太多,否则会内存溢出

  3. 递归所体现的思想:拆分合并的思想

例1:求5的阶乘

public class Demo10 {
    public static void main(String[] args) {
       int num= add(5);
        System.out.println(num);
    }
    public static int  add(int i){
        if (i==1){
            return 1;
        }else
          return i*add(i-1);
    }
}

例2:有一对兔子,从出生后第3个月起每个月都生一对兔子,小兔子长到第三个月后每个月又生一对兔子,假如兔子都不死,问第二十个月的兔子对数为多少?
由此可见兔子对象的数据是:1 , 1 , 2 , 3 , 5 , 8 …

public class Demo11 {
    public static void main(String[] args) {
        int num=add(20);
        System.out.println(num);
    }
    public static int add(int i){
        if (i==1||i==2){
            return 1;
        }else
            return add(i-2)+add(i-1);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值