You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何操作数组及用数组存储元素?附Student类实现代码

嘿,我来帮你把数组操作的要点,结合你给的Student类实例理清楚~

数组的基本操作

数组是Java里用来存储同类型元素的固定长度容器,下面是日常开发中最常用的操作方式:

1. 创建数组

有两种实用的创建方式:

  • 声明时直接初始化元素:
// 存储字符串的科目数组
String[] subjects = {"Math", "English", "Computer Science"};
// 直接创建并初始化Student对象数组
Student[] students = {
    new Student("Alice", "Smith", subjects, 90),
    new Student("Bob", "Brown", new String[]{"Physics"}, 85)
};
  • 指定长度后逐个赋值(适合提前知道元素数量但暂时没数据的场景):
// 创建长度为3的Student数组,初始值都是null
Student[] studentArray = new Student[3];
// 后续逐个填充对象
studentArray[0] = new Student("Charlie", "Davis", new String[]{"Chemistry"}, 88);

2. 访问数组元素

通过索引(从0开始计数)直接访问,比如:

// 获取科目数组的第一个元素
String firstSubject = subjects[0];
// 获取studentArray里第一个学生的名字
String studentFirstName = studentArray[0].getfName();

3. 修改数组元素

同样通过索引定位后修改:

// 修改科目数组的第二个元素
subjects[1] = "Advanced English";
// 修改某个学生的科目数组
Student targetStudent = studentArray[0];
targetStudent.setSubject(new String[]{"Chemistry", "Biology"});

4. 遍历数组

三种常见遍历方式,按需选择:

  • 普通for循环(适合需要用到索引的场景):
for (int i = 0; i < subjects.length; i++) {
    System.out.println("第" + (i+1) + "门科目:" + subjects[i]);
}
  • 增强for循环(简化遍历,不需要关心索引):
for (Student stu : students) {
    System.out.println("学生姓名:" + stu.getfName() + " " + stu.getsName());
}
  • 借助Arrays工具类快速打印:
import java.util.Arrays;
// 直接打印科目数组的所有元素
System.out.println("科目列表:" + Arrays.toString(subjects));

5. 数组常用工具类

Java的java.util.Arrays类提供了超多便捷方法,比如:

  • Arrays.copyOf():复制数组,还能顺便扩容
  • Arrays.sort():对数组排序(自定义对象数组排序需要实现Comparable接口,你的Student类已经做了)
  • Arrays.asList():把数组转换成List集合,方便后续使用集合的API

结合你的Student类使用数组

首先我把你提供的Student类补全(setSubject方法没写完),还加了实用的toString和完整的compareTo实现:

import java.util.Arrays;

public class Student implements Comparable<Student> { 
    String fName; 
    String sName; 
    String subject[]; 
    int subjectValuation; 

    public Student(String fName, String sName, String[] subject, int subjectValuation) { 
        this.fName = fName; 
        this.sName = sName; 
        this.subject = subject; 
        this.subjectValuation = subjectValuation; 
    } 

    public String[] getSubject() { 
        return subject; 
    } 

    public String getfName(){ 
        return fName; 
    } 

    public String getsName(){ 
        return sName; 
    } 

    public void setSubject(String[] subject) { 
        this.subject = subject;
    }

    // 实现Comparable接口,按学科评分排序
    @Override
    public int compareTo(Student o) {
        return Integer.compare(this.subjectValuation, o.subjectValuation);
    }

    // 重写toString,方便打印学生完整信息
    @Override
    public String toString() {
        return "Student{" +
                "姓名='" + fName + " " + sName + '\'' +
                ", 科目=" + Arrays.toString(subject) +
                ", 学科评分=" + subjectValuation +
                '}';
    }
}

下面是使用数组存储Student对象,以及操作学生的subject数组的完整示例:

import java.util.Arrays;

public class Main {
    public static void main(String[] args) {
        // 1. 创建两个学生的科目数组
        String[] aliceSubjects = {"Math", "English", "Art"};
        String[] bobSubjects = {"Physics", "Computer Science"};

        // 2. 创建Student数组存储学生对象
        Student[] studentList = new Student[2];
        studentList[0] = new Student("Alice", "Johnson", aliceSubjects, 92);
        studentList[1] = new Student("Bob", "Williams", bobSubjects, 87);

        // 3. 遍历打印所有学生信息
        System.out.println("=== 初始学生列表 ===");
        for (Student stu : studentList) {
            System.out.println(stu);
        }

        // 4. 修改Alice的科目数组
        studentList[0].setSubject(new String[]{"Math", "Advanced English", "Art", "Music"});
        System.out.println("\n=== 修改Alice科目后 ===");
        System.out.println(studentList[0]);

        // 5. 按学科评分对学生数组排序
        Arrays.sort(studentList);
        System.out.println("\n=== 按评分排序后的学生列表 ===");
        for (Student stu : studentList) {
            System.out.println(stu);
        }
    }
}

小提醒

  • 数组长度是固定的,如果需要动态添加/删除元素,建议用ArrayList这类集合类,但如果元素数量固定,数组的性能会更优。
  • 数组存储对象时,存的是对象的引用,修改数组里对象的属性会直接影响原对象。
  • 自定义对象数组排序时,要么实现Comparable接口,要么使用Comparator自定义排序规则。

内容的提问来源于stack exchange,提问作者Bob Jhonson

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.26 08:35:12