0
点赞
收藏
分享

微信扫一扫

0304 复习方法的使用 数组的算法方法版 2101

一叶随风_c94d 2022-03-11 阅读 14

普通方法的定义

无参,无返回值

public static void 方法名(){
	方法体
}

普通方法的调用

方法名()

有参数,有返回值的方法


public static 返回数据的类型 方法名(参数类型1 参数名1,参数类型n 参数名n){
	方法体
	return 要返回的数据
}


调用带参,带返回值的方法

数据类型 变量 = 方法名(数据1,数据n)

方法 显示数组所有成员

public class test01显示数组所有内容 {
    public static void main(String[] args) {
        // 定义一个数组
        String[] strings = {"a", "b", "c", "d"};

        // 来一个方法,显示数组的所有内容
        showItems(strings);

        // 修改一下数组下标为2的数据,改成"k"
        strings[2] = "k";

        System.out.println("===修改后====");
        // 再次显示数组的成员
        showItems(strings);


    }

    // 定义方法
    public static void showItems(String[] items) {
        // 遍历方式打印输出成员内容
        for (int i = 0; i < items.length; i++) {
            // i相当于是下标了
            // 取数组的值:数组[下标]
            System.out.println(items[i]);
        }
    }

}

方法 获取数组中的成员个数

public class test02求数组的真实长度 {
    public static void main(String[] args) {
        String[] strings = new String[5];

        strings[0] = "a";
        strings[1] = "b";
        strings[2] = "c";
        // ["a","b",null,null,null]

        // 需要一个功能来帮我实现求真实长度的想法
        int memberNum = getMemberNum(strings);
        System.out.println("数组的真实长度是:" + memberNum);

        System.out.println("自己又添加了一个数据");
        strings[3] = "d";

        // 再一次查看它的真实长度
        memberNum = getMemberNum(strings);
        System.out.println("22数组的真实长度是:" + memberNum);

    }

    public static int getMemberNum(String[] items) {
        // 定义一个变量,记录真实长度
        int memberNum = 0;

        // 遍历这个数组,拿到下标所对应的数据
        // 判断数据是不是,不等于null
        // 如果数据不等于null,就让变量加1
        for (int i = 0; i < items.length; i++) {
            String v = items[i];
            if (v != null) {
                memberNum++;
            }
        }

        // 返回这个长度
        return memberNum;
    }
}

方法 判断成员是否存在

import java.util.Scanner;

public class test03判断成员是否存在 {
    public static void main(String[] args) {
        String[] strings = {"a", "b", "c", "d"};

        // 数据
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要核查的数据:");
        String c = scanner.next();

        // 核对成员是否存在
        boolean exist = isExist(strings, c);

        // 结果输出
        System.out.println("是否存在" + exist);

    }

    public static boolean isExist(String[] strings, String key) {
        // 定义一个变量,标记是否存在
        boolean exist = false;
        // 遍历数组
        for (int i = 0; i < strings.length; i++) {
            // 获取下标对应的数据
            String s = strings[i];
            // 判断当前的这个数据与要搜索的词是不是一样的
            // 如果一样,代表找到了
            if (s.equals(key)) {
                // 把标记变量设置为true
                exist = true;
                // 退出循环
                break;
            }
        }
        // 返回结果
        return exist;

    }
}

方法 未位添加算法

public class test04未位添加 {
    public static void main(String[] args) {
        // 定义一个数组
        String[] strings = new String[5];
        // 添加测试数据
        strings[0] = "a";
        System.out.println("操做前");
        showItems(strings);
        // 待添加数据
        String item1 = "k";
        String item2 = "p";
        // 第一次数组添加
        boolean success = appendItem(strings, item1);
        // 判断是否添加成功
        if (success) {
            System.out.println("添加成功");
        } else {
            System.out.println("添加失败");
        }
        // 查看数组的详情
        System.out.println("操做后");
        showItems(strings);

        System.out.println("再次添加");
        success = appendItem(strings, item2);
        System.out.println("操做后2");
        System.out.println("操做结果" + success);
        showItems(strings);

    }


    // 定义方法
    public static void showItems(String[] items) {
        // 遍历方式打印输出成员内容
        for (int i = 0; i < items.length; i++) {
            // i相当于是下标了
            // 取数组的值:数组[下标]
            System.out.println(items[i]);
        }
    }


    // 未位添加数组的编写码写法
    public static boolean appendItem(String[] strings, String item) {

        // 求真实长度,得到memberNum的变量
        int memberNum = getMemberNum(strings);

        // 判断数组是否已满,如果满了,就中止方法,如果没有满,断续往下走
        // 当数组已满的时候,我们不允许添加
        if (memberNum == strings.length) {
            return false;
        }

        // 数组[真实长度] = 要插入的数据
        strings[memberNum] = item;

        // 返回结果
        return true;
    }

    public static int getMemberNum(String[] strings) {

        // 定义一个变量记录长度
        int memberNum = 0;

        // 遍历数组
        for (int i = 0; i < strings.length; i++) {
            // 获取当前下标对应的数据
            String v = strings[i];
            // 判断是否不为null
            if (v != null) {
                memberNum++;
            }
        }

        //  返回长度
        return memberNum;

    }
}

方法 未位删除

public class test05未位删除 {
    public static void main(String[] args) {
        // 定义一个数组
        String[] strings = {"a", "b", "c", "d", "e"};

        // 显示数组成员
        System.out.println("操做前");
        showItems(strings);

        // 第一次未位删除
        boolean isSuccess = popItem(strings);
        if (isSuccess) {
            System.out.println("删除成功");
        } else {
            System.out.println("删除失败");
        }

        System.out.println("第二次未位删除");
        popItem(strings);

        System.out.println("第三次删除");
        popItem(strings);

        // 查看结果
        System.out.println("删除后");
        showItems(strings);


    }

    public static boolean popItem(String[] strings) {
        // 获取真实长度
        int memberNum = getMemberNum(strings);

        // 不操做的情况
        // 当真实长度为0的时候,无法删除
        // 中止方法,返回一个假
        if (memberNum == 0) {
            return false;
        }

        // 对于非空的数组我们要进行未位删除
        // 求最末位数据的下标
        int lastIndex = memberNum - 1;
        // 设置最末位的下标对应的数据为数组的默认值
        strings[lastIndex] = null;
        // 返回数据
        return true;
    }


    // 定义方法
    public static void showItems(String[] items) {
        // 遍历方式打印输出成员内容
        for (int i = 0; i < items.length; i++) {
            // i相当于是下标了
            // 取数组的值:数组[下标]
            System.out.println(items[i]);
        }
    }


    // 未位添加数组的编写码写法
    public static boolean appendItem(String[] strings, String item) {

        // 求真实长度,得到memberNum的变量
        int memberNum = getMemberNum(strings);

        // 判断数组是否已满,如果满了,就中止方法,如果没有满,断续往下走
        // 当数组已满的时候,我们不允许添加
        if (memberNum == strings.length) {
            return false;
        }

        // 数组[真实长度] = 要插入的数据
        strings[memberNum] = item;

        // 返回结果
        return true;
    }

    public static int getMemberNum(String[] strings) {

        // 定义一个变量记录长度
        int memberNum = 0;

        // 遍历数组
        for (int i = 0; i < strings.length; i++) {
            // 获取当前下标对应的数据
            String v = strings[i];
            // 判断是否不为null
            if (v != null) {
                memberNum++;
            }
        }

        //  返回长度
        return memberNum;
    }
}

作业

在这里插入图片描述

举报

相关推荐

0 条评论