Procházet zdrojové kódy

20250607_方法&API&力扣

WanJL před 3 dny
rodič
revize
c6bbcc4a26

+ 39 - 0
chapter01-javaSE/20250607_方法&API&力扣/Demo01.java

@@ -0,0 +1,39 @@
+package com.loveCoding.j20250607_method;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title Demo01
+ * @description 方法的重载
+ * @create 2025/6/7
+ */
+public class Demo01 {
+    /*
+        方法重载:指的是多个方法的【方法名相同】,但是【参数列表不同】,就形成了重载
+        ps:方法的返回值不参与重载。
+     */
+    public static int add(int a,int b){
+        return a+b;
+    }
+    //返回值类型不参与重载,也就是说返回值是什么对于重载来说无所谓。
+    public static String add(double a,int b){
+        return "";
+    }
+    public static int add(int a,int b,int c){
+        return a+b+c;
+    }
+    public static int add(int a,int b,int c,int d){
+        return a+b+c+d;
+    }
+    public static int add(int a,int b,int c,int d,int e){
+        return a+b+c+d+e;
+    }
+
+    public static int add(int a,int b,int c,int d,int e,int f){
+        return a+b+c+d+e+f;
+    }
+    // 我们使用main方法调用我们自己定义的方法,其实是站在用户的角度使用我们定义的方法程序。
+    public static void main(String[] args) {
+        add(1,2,3,4,5,6);
+    }
+}

+ 40 - 0
chapter01-javaSE/20250607_方法&API&力扣/Demo02.java

@@ -0,0 +1,40 @@
+package com.loveCoding.j20250607_method;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title Demo02
+ * @description
+ * @create 2025/6/7
+ */
+public class Demo02 {
+    /**
+     * 登录的方法
+     * @param username 用户名
+     * @param password 密码
+     * @return 登录结果
+     */
+    public static String login(String username,String password){
+        return "";
+    }
+
+    /**
+     * 登录的方法,
+     * @param phone 手机号
+     * @param code 验证码
+     * @return
+     */
+    public static String login(String phone,int code){
+        return "";
+    }
+
+    /**
+     * 登录的方法
+     * @param code 二维码登录
+     * @return
+     */
+    public static String login(String code){
+        return "";
+    }
+
+}

+ 17 - 0
chapter01-javaSE/20250607_方法&API&力扣/Demo03.java

@@ -0,0 +1,17 @@
+package com.loveCoding.j20250607_method;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title Demo03
+ * @description
+ * @create 2025/6/7
+ */
+public class Demo03 {
+    public static int subtraction(int a,int b){
+        return a-b;
+    }
+    public static int subtraction(int a,int b,int c){
+        return a-b-c;
+    }
+}

+ 57 - 0
chapter01-javaSE/20250607_方法&API&力扣/Demo04.java

@@ -0,0 +1,57 @@
+package com.loveCoding.j20250607_method;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title T5
+ * @description 5.编写一个遍历数组的方法。思考是否要有参数,参数类型是什么,是否要有返回值,如果有返回值类型是什么。
+ * @create 2025/6/7
+ */
+public class Demo04 {
+    //5.编写一个遍历数组的方法。思考是否要有参数,参数类型是什么,是否要有返回值,如果有返回值类型是什么。
+
+
+
+    public static void method(int a){
+        a=a+100;
+    }
+
+    public static void method2(int[] arr){
+        //arr=new int[]{15,3};
+        arr[0]=15;
+        arr[1]=666;
+        arr[2]=7777;
+    }
+    public static void foreachArray(int[] arr){
+        for (int i = 0; i < arr.length; i++) {
+            System.out.print(arr[i]+"\t");
+        }
+    }
+
+    public static void main(String[] args) {
+
+
+        /*
+            调用方法前定义的基本数据类型变量,调用方法时传入方法。无论做任何操作,都不影响外部定义的变量。
+            因为i调用方法时传递是【基本数据类型】的副本,不影响原始值。这种传递参数的方式叫“值传递”
+         */
+        int a=1;
+        System.out.println(a);
+        method(a);
+        System.out.println(a);
+
+
+        //引用数据类型传递作为参数的时候,也是【值传递】
+        //也是将实参的副本传递进方法。但是引用数据类型的值是堆内存的地址。
+        //所以复制的副本,其实也是一个地址。两个地址访问的其实是同一个堆内存的位置。
+        //所以,一个进行了修改,另一个也会受到影响。
+
+        int[] arr={1,3,4,5,7,8,9,6,21};
+        foreachArray(arr);  //1,3,4,5,7,8,9,6,21
+        method2(arr);
+        System.out.println();
+        foreachArray(arr);//15,3,4,5,7,8,9,6,21
+
+
+    }
+}

+ 37 - 0
chapter01-javaSE/20250607_方法&API&力扣/Demo05.java

@@ -0,0 +1,37 @@
+package com.loveCoding.j20250607_method;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title Demo05
+ * @description Math类,数学运算类的使用
+ * @create 2025/6/7
+ */
+public class Demo05 {
+    public static void main(String[] args) {
+        //通过Math.abs()方法,获取一个数字的绝对值
+        int absNum1 = Math.abs(-5);
+        System.out.println(absNum1);
+        //返回大于或等于参数的最小double值,等于一个整数
+        double ceil = Math.ceil(3);
+        System.out.println(ceil);
+        //返回小于或等于参数的最大double值,等于一个整数
+        double floor = Math.floor(3.999);
+        System.out.println(floor);
+        //按照四舍五入返回最接近参数的int
+        long round = Math.round(3.54);
+        System.out.println(round);
+        //比较两个数,返回最大值
+        int max = Math.max(3, 4);
+        System.out.println(max);
+        //比较两个数,返回最小值
+        int min = Math.min(3, 4);
+        System.out.println(min);
+        //返回a的b次幂的值
+        double pow = Math.pow(15, 3);
+        System.out.println(pow);
+        //返回值为double的正值,范围是[0.0,1.0)
+        double random = Math.random();
+        System.out.println(random);
+    }
+}

+ 19 - 0
chapter01-javaSE/20250607_方法&API&力扣/Demo06.java

@@ -0,0 +1,19 @@
+package com.loveCoding.j20250607_method;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title Demo06
+ * @description
+ * @create 2025/6/7
+ */
+public class Demo06 {
+    public static void main(String[] args) {
+        for (int i = 0; i < 50; i++) {
+            System.out.println(i);
+            if (i==25){
+                System.exit(666);
+            }
+        }
+    }
+}

+ 22 - 0
chapter01-javaSE/20250607_方法&API&力扣/Demo07.java

@@ -0,0 +1,22 @@
+package com.loveCoding.j20250607_method;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title Demo07
+ * @description 返回当前的时间-毫秒值
+ * @create 2025/6/7
+ */
+public class Demo07 {
+    public static void main(String[] args) {
+        //开始时间
+        long start = System.currentTimeMillis();
+        for (int i = 0; i < 50; i++) {
+            System.out.println(i);
+        }
+        //结束时间
+        long end = System.currentTimeMillis();
+        //结束时间-开始时间=运行时间
+        System.out.println(end-start);
+    }
+}

+ 22 - 0
chapter01-javaSE/20250607_方法&API&力扣/Demo08.java

@@ -0,0 +1,22 @@
+package com.loveCoding.j20250607_method;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title Demo08
+ * @description Object类 所有类的直接父类或间接父类
+ * @create 2025/6/7
+ */
+public class Demo08 {
+
+
+    public static void main(String[] args) {
+        int[] arr={16,15};
+        System.out.println(arr.toString());
+        System.out.println(arr);
+
+        String s1="hello world";
+        System.out.println(s1.toString());
+        System.out.println(s1);
+    }
+}

+ 102 - 0
chapter01-javaSE/20250607_方法&API&力扣/Demo09.java

@@ -0,0 +1,102 @@
+package com.loveCoding.j20250607_method;
+
+import java.util.Arrays;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title Demo09
+ * @description
+ * @create 2025/6/7
+ */
+public class Demo09 {
+    /**
+     * 1、实现将arr1和arr2数组合并为一个新的数组并返回,先数组1后数组2
+     * 1,2,3,4,5   5,4,3,2,1
+     * 1,2,3,4,5,5,4,3,2,1
+     *
+     * @param arr1
+     * @param arr2
+     * @return
+     */
+    public static int[] toOneArray(int[] arr1, int[] arr2) {
+        if (arr1.length == 0 || arr1 == null) {    //如果前一个数组为空,则直接返回后一个数组
+            return arr2;
+        }
+        if (arr2.length == 0 || arr2 == null) {    //如果后一个数组为空,则直接返回前一个数组
+            return arr1;
+        }
+        int[] tempArray = new int[arr1.length + arr2.length];
+        for (int i = 0; i < tempArray.length; i++) {
+            if (i < arr1.length) {
+                tempArray[i] = arr1[i];
+            } else {
+                tempArray[i] = arr2[i - arr1.length];
+            }
+        }
+        return tempArray;
+    }
+    /*
+        2、实现将arr1和arr2数组合并为一个新的数组并返回,
+        起始元素为数组1的元素,然后是数组2的第1个元素....以此类推
+        1,2,3,4,5   5,4,3,2,1
+        1,5,2,4,3,3,4,2,5,1
+     */
+
+    public static int[] toOneArray2(int[] arr1, int[] arr2) {
+        //如果前一个数组为空,则直接返回后一个数组
+        if (arr1.length == 0 || arr1 == null) {
+            return arr2;
+        }
+        //如果后一个数组为空,则直接返回前一个数组
+        if (arr2.length == 0 || arr2 == null) {
+            return arr1;
+        }
+
+        //新数组长度是两个数组的长度和
+        int[] tempArray = new int[arr1.length + arr2.length];
+        //判断两个数组哪个长度更短
+        int minLength= arr1.length> arr2.length?arr2.length:arr1.length;
+        //以长度短的进行循环
+        for (int i = 0; i < minLength; i++) {
+            tempArray[i*2]=arr1[i]; //先赋值数组arr1     0 2 4 6 8 10 .....
+            tempArray[(i*2)+1]=arr2[i]; //再赋值数组arr2 1 3 5 7 9 11.....
+        }
+        //再开启一个循环,把数组长的剩余的元素依次放到新数组后面
+        for (int i = minLength*2; i <tempArray.length ; i++) {
+            tempArray[i]=arr1.length< arr2.length?arr2[i-minLength]:arr1[i-minLength];
+        }
+        return tempArray;
+    }
+
+
+    /*
+    给定一个整数数组 nums 和一个整数目标值 target,请你在该数组中找出
+        和为目标值 target  的那 两个 整数,并返回它们的数组下标。
+        你可以假设每种输入只会对应一个答案,并且你不能使用两次相同的元素。
+        你可以按任意顺序返回答案。
+     */
+    public int[] twoSum(int[] nums, int target) {
+        int[] arr=new int[2];
+        for (int i = 0; i < nums.length; i++) {
+            for (int j = i+1; j < nums.length; j++) {
+                if (nums[i]!=nums[j]&&nums[i]+nums[j]==target){
+                    arr[0]=i;
+                    arr[1]=j;
+                    return arr;
+                }
+            }
+        }
+        return arr;
+    }
+
+
+
+    public static void main(String[] args) {
+        int[] arr1 = {1, 2, 3};
+        int[] arr2 = {10, 20, 30, 40, 50};
+        int[] array = toOneArray2(arr1, arr2);
+        System.out.println(Arrays.toString(array));
+
+    }
+}

+ 187 - 0
chapter01-javaSE/20250607_方法&API&力扣/MyArray.java

@@ -0,0 +1,187 @@
+package com.loveCoding.j20250607_method;
+
+import java.util.Arrays;
+
+/**
+ * @author WanJl
+ * @version 1.0
+ * @title MyArray
+ * @description
+ * @create 2025/6/7
+ */
+public class MyArray {
+    static int length=0;
+    static int[] myArray=new int[length];
+
+    /**
+     * 返回数组长度的方法
+     */
+    public static int size(){
+        return length;
+    }
+
+    /**
+     * 判断数组是否为空的方法
+     * 数组为空,代表两个含义:
+     *  1、数组长度为0
+     *  2、数组为null
+     *  @return true就是为空,false就是不为空
+     */
+    public static boolean isEmpty(){
+        return myArray.length==0||myArray==null||length==0?true:false;
+    }
+
+    /**
+     * 判断数组不为空
+     * @return true就是不为空,false就是为空
+     */
+    public static boolean isNotEmpty(){
+        return !isEmpty();
+    }
+
+
+
+    /**
+     * 该方法用来像myArray数组中添加一个元素,
+     * 并且数组长度会随着添加元素的个数进行变化。
+     * @param element
+     */
+    public static void add(int element){
+        //1、建立一个临时数组,临时数组的长度是旧数组长度+1
+        int[] tempArray=new int[length+1];
+        //2、把原来的数组中的元素复制到新数组中
+        for (int i = 0; i < myArray.length; i++) {
+            tempArray[i]=myArray[i];
+        }
+        //3、把新元素添加到新数组中最后一位
+        tempArray[tempArray.length-1]=element;
+        //4、用新数组替换掉旧的数组,赋值给myArray
+        myArray=tempArray;
+        //5、把数组长度赋值给length属性
+        length=myArray.length;
+    }
+
+    /**
+        编写一个方法,调用该方法,删除数组中最后一个元素
+     */
+    public static void removeLast(){
+        //1、创建新的临时数组
+        int[] tempArray=new int[length-1];
+        //2、把原来的数组中的元素复制到新数组中
+        for (int i = 0; i < tempArray.length; i++) {
+            tempArray[i]=myArray[i];
+        }
+        //3、用新数组替换掉旧的数组,赋值给myArray
+        myArray=tempArray;
+        //4、把数组长度赋值给length属性
+        length=myArray.length;
+    }
+    /**
+        编写一个方法,调用该方法,删除数组中第一个元素
+     */
+    public static void removeFirst(){
+        //1、创建新的临时数组
+        int[] tempArray=new int[length-1];
+        //2、把原来的数组中的元素复制到新数组中
+        for (int i = 0; i < tempArray.length; i++) {
+            tempArray[i]=myArray[i+1];
+        }
+        //3、用新数组替换掉旧的数组,赋值给myArray
+        myArray=tempArray;
+        //4、把数组长度赋值给length属性
+        length=myArray.length;
+    }
+
+    /**
+     * 向指定位置添加元素
+     * @param element 元素值
+     * @param index 索引值
+     */
+    public static void add(int element,int index){
+        //1、创建新的临时数组
+        int[] tempArray=new int[length+1];
+        //2、把原来的数组中的元素复制到新数组中
+        for (int i = 0; i < myArray.length; i++) {
+            if(i<index){
+                tempArray[i]=myArray[i];
+            }else{
+                tempArray[i+1]=myArray[i];
+            }
+        }
+        //在指定位置添加元素
+        tempArray[index]=element;
+        //3、用新数组替换掉旧的数组,赋值给myArray
+        myArray=tempArray;
+        //4、把数组长度赋值给length属性
+        length=myArray.length;
+    }
+
+
+
+    /**
+     * 根据索引值删除数组中指定元素
+     * @param index
+     */
+    public static void remove(int index){
+        //1、创建新的临时数组
+        int[] tempArray=new int[length-1];
+        //2、把原来的数组中的元素复制到新数组中
+        for (int i = 0; i < tempArray.length; i++) {
+            if (i<index){
+                tempArray[i]=myArray[i];
+            }else if (i>=index){
+                tempArray[i]=myArray[i+1];
+            }
+        }
+        //3、用新数组替换掉旧的数组,赋值给myArray
+        myArray=tempArray;
+        //4、把数组长度赋值给length属性
+        length=myArray.length;
+    }
+
+
+    /**
+     * 遍历输出数组的方法
+     * @param array
+     */
+    public static void printArray(int[] array){
+        System.out.print("[\t");
+        for (int i = 0; i < array.length; i++) {
+            System.out.print(array[i]+"\t");
+        }
+        System.out.println("]");
+    }
+
+    /**
+     * 获取指定元素,根据索引位置获取指定的元素
+     * @param index
+     */
+    public static int get(int index){
+        return myArray[index];
+    }
+
+
+
+
+    public static void main(String[] args) {
+
+
+
+        MyArray.add(15);
+        MyArray.add(66);
+        MyArray.add(77);
+        MyArray.add(88);
+        MyArray.add(10);
+        MyArray.add(2);
+        //printArray(MyArray.myArray);
+        System.out.println(Arrays.toString(MyArray.myArray));
+        //MyArray.removeLast();
+        //System.out.println(Arrays.toString(MyArray.myArray));
+        //MyArray.removeFirst();
+        //System.out.println(Arrays.toString(MyArray.myArray));
+        //MyArray.remove(5);
+        MyArray.add(9999,3);
+        System.out.println(Arrays.toString(MyArray.myArray));
+    }
+
+}