对象数组的练习

public class StudentTest {
    public static void main(String[] args) {
        Student[] stus = new Student[20]; //声明Student类型的数组(这只是创建数组,因为是引用数据类型(只能存放null或者地址),还没创建对象,所以里面存放的是null)
        for (int i = 0; i < 20; i++) {
            stus[i] = new Student();//(这里是创建了对象,此时引用数据类型里存放的是对象的地址。)附图一
            stus[i].number = i + 1;//给student对象的属性赋值
            stus[i].state = (int) (Math.random() * 6 + 1);//年级[1,6]
            stus[i].score = (int) (Math.random() * 100 + 1);//成绩[0,100]
        }
        for (int i = 0; i < stus.length; i++) {//遍历学生数组
            //System.out.println(stus[i]);//这样输出的就只是地址!!!!
            //方式一输出:
            // System.out.println(stus[i].number + "," + stus[i].state + "," + stus[i].score);
            //方式二输出
            System.out.println(stus[i].info());//??????????????????????????????????????
        }
        System.out.println("------------------------------------");
//问题一:打印出3年级(state值为3)的学生信息。
        for (int i = 0; i <stus.length ; i++) {
            if(stus[i].state==3){
                System.out.println(stus[i].info());
            }
        }
        System.out.println("------------------------------------");
        //问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息。
        for (int i = 0; i <stus.length-1 ; i++) {
            for (int j = 0; j <stus.length-1-i ; j++) {
                if(stus[j].score>stus[j+1].score)
                {//如果需要换序,交换的是数组的元素。此时的元素就是Student的对象。
                    Student temp=stus[j];
                    stus[j]=stus[j+1];
                    stus[j+1]=temp;
                }
            }
        }
        for (int i = 0; i < stus.length; i++){
            System.out.println(stus[i].info());
        }
    }
}
class Student{
    //属性
    int number;//学号
    int state;//年级
    int score;//成绩

//显示学生信息的方法
    public String info() {
        return "学号:"+number+",年级:"+state+",成绩:"+score;
    }
}

 

-------------------------------对StudentTest1的优化。将操作数组的功能封装到方法中

public class StudentTest1 {
    public static void main(String[] args) {
        Student1[] stus = new Student1[20]; //声明Student类型的数组(这只是创建数组,因为是引用数据类型(只能存放null或者地址),还没创建对象,所以里面存放的是null)
        for (int i = 0; i < 20; i++) {
            stus[i] = new Student1();//(这里是创建了对象,此时引用数据类型里存放的是对象的地址。)//附图1
            stus[i].number = i + 1;//给student对象的属性赋值
            stus[i].state = (int) (Math.random() * 6 + 1);//年级[1,6]
            stus[i].score = (int) (Math.random() * 100 + 1);//成绩[0,100]
        }

        StudentTest1 test=new StudentTest1();//因为方法封装在了StudentTest1里面,main的外面,所以main里想要调用方法需要重新造新对象。第五行的对象是针对下面的Student所建造的对象。

        test.print(stus);//遍历学生数组
        System.out.println("------------------------------------");

        test.searchState(stus,3);//问题一:打印出3年级(state值为3)的学生信息。

        System.out.println("------------------------------------");

        test.sort(stus);//问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息。
        test.print(stus);

    }
    public void print(Student1[] stus){
        for (int i = 0; i < stus.length; i++) {//遍历学生数组
            //System.out.println(stus[i]);//这样输出的就只是地址!!!!
            //方式一输出:
            // System.out.println(stus[i].number + "," + stus[i].state + "," + stus[i].score);
            //方式二输出
            System.out.println(stus[i].info());//??????????????????????????????????????
        }
    }

    /**
     * 查找Stduent1数组中指定年级的学生信息
     * @param stus   要查找的数组
     * @param needState   要查找的年级
     */
    public void searchState(Student1[] stus,int needState){
        //问题一:打印出3年级(state值为3)的学生信息。
        for (int i = 0; i <stus.length ; i++) {
            if(stus[i].state==needState){
                System.out.println(stus[i].info());
            }
        }
    }

    /**
     *    给Student1数组排序
     * @param stus
     */
    public void sort(Student1[]stus){
        //问题二:使用冒泡排序按学生成绩排序,并遍历所有学生信息。
        for (int i = 0; i <stus.length-1 ; i++) {
            for (int j = 0; j <stus.length-1-i ; j++) {
                if(stus[j].score>stus[j+1].score)
                {//如果需要换序,交换的是数组的元素。此时的元素就是Student的对象。
                    Student1 temp=stus[j];
                    stus[j]=stus[j+1];
                    stus[j+1]=temp;
                }
            }
        }
    }
}
class Student1{
    //属性
    int number;//学号
    int state;//年级
    int score;//成绩

    //显示学生信息的方法
    public String info() {
        return "学号:"+number+",年级:"+state+",成绩:"+score;
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
PTA(Programming Test Assessment)是一个在线编程测试和评估平台,可以用于程序设计教学和招聘面试。在PTA上,可以练习各种编程题目,包括面向对象编程的题目。 要创建学生类和对象数组,可以按照以下步骤进行: 1. 定义学生类:首先需要定义一个学生类,包含学生的姓名、学号、年级、成绩等属性,以及相应的构造函数和成员函数。 ```cpp class Student { public: string name; int id; int grade; int score; Student(string name, int id, int grade, int score) { this->name = name; this->id = id; this->grade = grade; this->score = score; } void display() { cout << name << " " << id << " " << grade << " " << score << endl; } }; ``` 2. 创建对象数组:在主函数中创建对象数组,可以使用循环语句逐个输入学生信息,或者从文件中读取信息。 ```cpp int main() { const int MAX_STUDENT = 100; Student students[MAX_STUDENT]; int n; cin >> n; for (int i = 0; i < n; i++) { string name; int id, grade, score; cin >> name >> id >> grade >> score; students[i] = Student(name, id, grade, score); } for (int i = 0; i < n; i++) { students[i].display(); } return 0; } ``` 在上面的代码中,我们定义了一个常量MAX_STUDENT表示最大的学生数,创建了一个名为students的对象数组,并使用循环语句逐个输入学生信息。最后,我们调用每个学生对象的display()函数显示学生信息。 注意,当对象数组被创建时,每个对象都会自动调用构造函数,以初始化对象的属性。在本例中,我们将学生信息作为参数传递给构造函数,以便在创建对象时进行初始化。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值