Java 数组示例:使用 Student 对象存储学生信息并遍历输出
public static void main(String[] args) {\n Student[] students = new Student[5]; // 定义一个数组\n students[0] = new Student(1, "张三", 99);\n students[1] = new Student(2, "王五", 90);\n students[2] = new Student(3, "赵六", 95);\n students[3] = new Student(4, "小明", 90);\n students[4] = new Student(5, "师姐", 98);\n\n for (Student s : students) { // 遍历数组中的数据\n System.out.println(s);\n }\n\n详解内容:这段代码定义了一个包含5个元素的Student数组,并给数组中的每个元素赋值。然后使用增强型for循环遍历数组中的每个元素,并通过调用toString()方法打印每个Student对象的信息。\n\nStudent类的构造方法接受三个参数:学生ID、姓名和分数。每个元素都是通过调用Student类的构造方法创建的。\n\n最后,使用增强型for循环遍历数组中的每个元素,并通过调用System.out.println()方法打印出每个元素的信息。
原文地址: https://www.cveoy.top/t/topic/pTRb 著作权归作者所有。请勿转载和采集!