public class Student {
    private String name;
    private int score;

    public Student(String name, int score) {
        this.name = name;
        this.score = score;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getScore() {
        return score;
    }

    public void setScore(int score) {
        this.score = score;
    }
}
public class Main {
    public static void main(String[] args) {
        String s = '张三:98,赵四:78,王五:69,朱红:78,刘川:59,秦龙:88';
        String[] students = s.split(',');
        List<Student> studentList = new ArrayList<>();

        for (String student : students) {
            String[] info = student.split(':');
            String name = info[0];
            int score = Integer.parseInt(info[1]);
            Student s = new Student(name, score);
            studentList.add(s);
        }

        for (Student student : studentList) {
            System.out.println('姓名:' + student.getName() + ',成绩:' + student.getScore());
        }
    }
}
Java 字符串解析创建学生对象并存储到集合中

原文地址: https://www.cveoy.top/t/topic/n1AL 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录