English 中文(简体)
究竟是建立一系列目标还是建立数据成员作为阵列,是哪一种好的做法?
原标题:Which is a good approach either to create an array of object or to create data members as an array?

I have used two approaches to solve the Question Here, Both Works But what i need to know is Which will be the Best to do Should I create an Array of Object or Should I create data members as an array ?

// 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("
");
        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("
The Student with Lowest CGPA is: ");
        System.out.println("Name: " + name[min] + "
Roll: " + roll[min] + "
CGPA: " + cgpa[min]);

    }

    void display(int i, String name, int roll, float cgpa) {
        System.out.println("Detail of Student: " + i);
        System.out.println("Name: " + name + "
Roll: " + roll + "
CGPA: " + 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 + "
Roll: " + student.roll + "
CGPA: " + 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]);
    }
}

Here, I have created Array of Object Both program runs fine but Which will be the Best Approach to do ???

问题回答

Maintaining a group of parallel arrays is annoying and troublesome.

如果使用

界定每个学生的班级。 如果这一类数据的目的是透明地传送:https://en.wikipedia.org/wiki/Immutable_object”rel=“nofollow noretinger”>,可转换成record

record Student ( String name , double grade ) {}

打击学生目标并收集。 您可以为此使用一个阵列,但总体上可以更好地利用Java Collections

List< Student > classroom = 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 ;
        }
    }
}

注: 该法典忽视了与最低年级挂钩的学生问题。

在更先进的 Java,使用流体、Comparator和一种方法参照。 见https://stackoverflow.com/q/36775518/642706>。 问题

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

无需<代码>方法。 记录中暗中实施的<编码>至String方法基本上符合你的愿望。

结果:

最低 Grade = 学生[名称=Bob,年级=2.8]

For clarity, separate your main method outside of your Student class. Make a class named something like Main or App. And move the user-interface code into its own method.





相关问题
Spring Properties File

Hi have this j2ee web application developed using spring framework. I have a problem with rendering mnessages in nihongo characters from the properties file. I tried converting the file to ascii using ...

Logging a global ID in multiple components

I have a system which contains multiple applications connected together using JMS and Spring Integration. Messages get sent along a chain of applications. [App A] -> [App B] -> [App C] We set a ...

Java Library Size

If I m given two Java Libraries in Jar format, 1 having no bells and whistles, and the other having lots of them that will mostly go unused.... my question is: How will the larger, mostly unused ...

How to get the Array Class for a given Class in Java?

I have a Class variable that holds a certain type and I need to get a variable that holds the corresponding array class. The best I could come up with is this: Class arrayOfFooClass = java.lang....

SQLite , Derby vs file system

I m working on a Java desktop application that reads and writes from/to different files. I think a better solution would be to replace the file system by a SQLite database. How hard is it to migrate ...

热门标签