java基础学习Day09练习(基础)

第二题:分析以下需求并实现

1.创建一个ArrayList集合,用于存储一些字符串
2.通过键盘录入5个字符串,保存到集合中
3.遍历集合,打印每一个字符串

import java.util.ArrayList;
import java.util.Scanner;

public class Test1 {
    public static void main(String[] args) {
        ArrayList<String> arr = new ArrayList<String>();
        Scanner sc = new Scanner(System.in);
        for (int i = 0; i < 5; i++) {
            System.out.println("请输入第" + (i + 1) + "个字符串:");
            String line = sc.nextLine();
            arr.add(line);
        }
        for (int i = 0; i < arr.size(); i++) {
            System.out.println(arr.get(i));
        }
    }
}

第三题:分析以下需求并实现

1.创建一个ArrayList集合,用于存储一些字符串:“abc”,“def”,“def”,“ghi”,“def”,“hij”,“jkol”
2.遍历集合,统计集合中"def"字符串一共出现了多少个
3.将集合中的所有"def"字符串删除。打印删除后的集合元素

public class Test2 {
    public static void main(String[] args) {
        ArrayList<String> arr = new ArrayList<String>();
        arr.add("abc");
        arr.add("def");
        arr.add("def");
        arr.add("ghi");
        arr.add("def");
        arr.add("hij");
        arr.add("jkol");
        
        //定义一个变量存储def出现的次数
        int num = getCount(arr);
        System.out.println("def 字符串一共出现了:" + num + "次");

        //删除def字符串
        rmFunc(arr, "def");
        System.out.println("删除后的集合为:");
        //遍历打印集合
        for (int i = 0; i < arr.size(); i++) {
            System.out.print(arr.get(i)+" ");
        }
    }

    public static int getCount(ArrayList<String> arr) {
        //定义一个变量存储def出现的次数
        int num = 0;
        for (int i = 0; i < arr.size(); i++) {
            if (arr.get(i) == "def") {
                num++;
            }
        }
        return num;
    }

    public static void rmFunc(ArrayList<String> arr, String s) {
        while (true) {
            boolean flag = arr.remove(s);
            if (!flag) {
                break;
            }
        }
    }
}

第四题:分析以下需求并实现

1.定义"学员"类Student,类中包含以下成员:
成员属性:
snum (学号):int类型, score(成绩):double类型,属性使用private修饰
为所有属性提供set/get方法。
2.定义类:Test06,类中定义main()方法,依次完成以下要求:
2.1 实例化3个Student对象,3个对象的属性分别为:1001, 55.0、1002, 66.6、1003, 77.0
2.2 创建一个ArrayList集合,分别将上面的3个Student对象添加到集合中
2.3 遍历集合,删除集合中元素的学号为1002的元素,并打印集合中剩余元素的学号和成绩

/*
学生类
 */
public class Student {
    private int snum;
    private double score;

    public Student() {
    }

    public Student(int snum, double score) {
        this.snum = snum;
        this.score = score;
    }

    public int getSnum() {
        return snum;
    }

    public void setSnum(int snum) {
        this.snum = snum;
    }

    public double getScore() {
        return score;
    }

    public void setScore(double score) {
        this.score = score;
    }
}
/*
测试类
 */
import java.util.ArrayList;

public class StudentDemo {
    public static void main(String[] args) {
        Student s1 = new Student(1001, 55.0);
        Student s2 = new Student(1002, 66.0);
        Student s3 = new Student(1003, 77.0);

        //创建集合存储学生对象
        ArrayList<Student> arr = new ArrayList<Student>();
        arr.add(s1);
        arr.add(s2);
        arr.add(s3);

        //遍历集合删除1002

        for (int i = 0; i < arr.size(); i++) {
            Student s = arr.get(i);
            if (s.getSnum() == 1002) {
                //此处有玄机,好好想想哦
                arr.remove(i--);
            } else {
                System.out.println(s.getSnum() + ":" + s.getScore());
            }
        }

    }
}
发布了68 篇原创文章 · 获赞 59 · 访问量 2316

猜你喜欢

转载自blog.csdn.net/Ruoice/article/details/103217235