您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

在具有属性的arraylist中创建新对象

在具有属性的arraylist中创建新对象

您需要的是以下内容

import java.util.*;

class TestStudent
{
    public static void main(String args[])
    {
        List<Student> StudentList= new ArrayList<Student>();
        Student tempStudent = new Student();
        tempStudent.setName("Rey");
        tempStudent.setIdNum(619);
        StudentList.add(tempStudent);
        System.out.println(StudentList.get(0).getName()+", "+StudentList.get(0).getId());
    }
}

class Student
{
    private String fname;
    private int stId;

    public String getName()
    {
        return this.fname;
    }

    public int getId()
    {
        return this.stId;
    }

    public boolean setName(String name)
    {
        this.fname = name;
        return true;
    }

    public boolean setIdNum(int id)
    {
        this.stId = id;
        return true;
    }
}
其他 2022/1/1 18:27:13 有515人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶