创建对象数组或将数据成员创建为数组哪种好方法?

问题描述 投票:0回答:1

我使用了两种方法来解决这个问题 在这里,两者都有效 但我需要知道的是 哪一个是最好的 我应该创建一个对象数组吗 或者 我应该将数据成员创建为数组吗?

// Write a program in java to input and display the details of n number of students having roll, name and cgpa as data members. Also display the name of the student having lowest cgpa.

import java.util.Scanner;

public class _3 {
    int n;
    int roll[];
    String name[];
    float cgpa[];

    _3(int n, int roll[], String name[], float cgpa[]) {
        this.roll = new int[n];
        this.name = new String[n];
        this.cgpa = new float[n];
        this.n = n;
        this.roll = roll;
        this.name = name;
        this.cgpa = cgpa;

    }

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the No. of Students to store data of: ");
        int num = sc.nextInt();
        int roll[] = new int[num];
        String name[] = new String[num];
        float cgpa[] = new float[num];
        for (int i = 0; i < num; i++) {
            System.out.println("Enter the Detail of Student : " + i);
            sc.nextLine();
            System.out.print("Name: ");
            name[i] = sc.nextLine();
            System.out.print("Roll: ");
            roll[i] = sc.nextInt();
            System.out.print("CGPA: ");
            cgpa[i] = sc.nextFloat();
        }
        _3 obj = new _3(num, roll, name, cgpa);
        System.out.println("\n");
        System.out.println("Student Details are: ");
        for (int i = 0; i < num; i++) {
            obj.display(i, name[i], roll[i], cgpa[i]);
        }
        sc.close();

        int min = obj.lowest(cgpa);
        System.out.println("\nThe Student with Lowest CGPA is: ");
        System.out.println("Name: " + name[min] + "\nRoll: " + roll[min] + "\nCGPA: " + cgpa[min]);

    }

    void display(int i, String name, int roll, float cgpa) {
        System.out.println("Detail of Student: " + i);
        System.out.println("Name: " + name + "\nRoll: " + roll + "\nCGPA: " + cgpa);
    }

    int lowest(float arr[]) {
        int min = 0;
        for (int i = 0; i < arr.length; i++) {
            if (arr[i] < arr[min]) {
                min = i;
            }
        }
        return min;
    }
}

在这里,我将数据成员创建为数组

//// Write a program in java to input and display the details of n number of students having roll, name and cgpa as data members. Also display the name of the student having lowest cgpa.

import java.util.Scanner;

public class Student_3 {


    int roll;
    String name;
    float cgpa;

    Student_3(int roll, String name, float cgpa) {
        this.roll = roll;
        this.name = name;
        this.cgpa = cgpa;
    }

    public static void main(String args[]) {
        Scanner sc = new Scanner(System.in);
        System.out.print("Enter the No. Of Students to store the Data of: ");
        int num = sc.nextInt();

        Student_3 student[] = new Student_3[num];
        int roll;
        String name;
        float cgpa;

        for (int i = 0; i < num; i++) {
            System.out.println("Enter the Details of Student No: " + (i + 1));
            System.out.print("Name: ");
            sc.nextLine();
            name = sc.nextLine();
            System.out.print("Roll No.: ");
            roll = sc.nextInt();
            System.out.print("CGPA: ");
            cgpa = sc.nextFloat();

            student[i] = new Student_3(roll, name, cgpa);
        }
        sc.close();

        // Displaying the Detail of Students
        System.out.println();
        for (int i = 0; i < num; i++) {
            student[i].display(i, student[i]);
        }

        // Lowest CGPA
        student[0].lowest_cgpa(student);
    }

    void display(int i, Student_3 student) {
        System.out.println("Details of Student No: " + (i + 1));
        System.out.println("Name: " + name + "\nRoll: " + student.roll + "\nCGPA: " + student.cgpa);


    }

    void lowest_cgpa(Student_3 student[]) {
        int min = 0;
        for (int i = 0; i < student.length; i++) {
            if (student[i].cgpa < student[min].cgpa) {
                min = i;
            }
        }
        System.out.println("The Student Details of lowest CGPA Holder is:");
        display(min, student[min]);
    }
}

在这里,我创建了对象数组 两个程序都运行良好 但 哪一个是最好的方法???

java arrays function optimization constructor
1个回答
0
投票

如果使用像 Java 这样的面向对象编程语言,你不妨使用对象。

定义一个班级来代表每个学生。如果此类的目的是透明地通信浅层不可变数据,则将该类设为record

record Student ( String name , double grade ) {}

实例化学生对象并收集。您可以为此使用数组,但通常最好使用 Java Collections

List< Student > class = new ArrayList<>();
class.add( new Student ( "Alice" , 3.3d ) ) ;
class.add( new Student ( "Bob" , 2.8d ) ) ;
class.add( new Student ( "Carol" , 3.1d ) ) ;

要找到最低的,你可以循环。

Student lowestGrade = null ;
for( Student student : class ) 
{
    if ( Objects.isNull( lowestGrade ) {
        lowestGrade = student ;
    } 
    else 
    {
        if ( student.grade() < lowestGrade.grade() ) 
        {
            lowestGrade = student ;
        }
    }
}

Note: This code ignores the issue of students tied for lowest grade.

In more advanced Java, use streams and method reference. See [this Question](https://stackoverflow.com/q/36775518/642706). 

```java
Student lowestGrade =
        classroom
                .stream ( )
                .min ( Comparator.comparing ( Student :: grade ) )
                .orElse ( null );

结果:

最低年级=学生[姓名=鲍勃,成绩=2.8]

© www.soinside.com 2019 - 2024. All rights reserved.