简体   繁体   English

如何将数据添加到二维数组?

[英]How to add data to a two-dimensional array?

I have to create a program that uses a two-dimensional array to store student names and their grades.我必须创建一个使用二维数组来存储学生姓名和成绩的程序。 The user inputs the students name and their grades and it is then supposed to be added.用户输入学生姓名和他们的成绩,然后应该添加。 It has to be able to hold data for 15 students.它必须能够保存 15 名学生的数据。 Can someone tell me how to add data to such an array?有人能告诉我如何将数据添加到这样的数组吗? I have attached my code so far, as well as my design preview.到目前为止,我已经附上了我的代码,以及我的设计预览。

public class StudentGrades extends javax.swing.JFrame {
    double [][] database = new double[4][15];
/**
 * Creates new form StudentGrades
 */
public StudentGrades() {
    initComponents();
    displayButton.setEnabled(false);
    studentButton.setEnabled(false);
    courseButton.setEnabled(false);
}

... ...

private void exitButtonActionPerformed(java.awt.event.ActionEvent evt) {                                           
    //set code to close program
    System.exit(0);
}                                          

private void addButtonActionPerformed(java.awt.event.ActionEvent evt) {                                          
    //clear output area
    outputArea.setText(null);

    //enable buttons
    displayButton.setEnabled(true);
    studentButton.setEnabled(true);
    courseButton.setEnabled(true);

    //declare variables
    double grade1 = Double.parseDouble(test1.getText());
    double grade2 = Double.parseDouble(test2.getText());
    double grade3 = Double.parseDouble(test3.getText());
    double grade4 = Double.parseDouble(test4.getText());

User Interface用户界面

Any addition of data into a two-dimensional array is based on the row and the column:将数据添加到二维数组中的任何操作都基于行和列:

means for 2d-array with size of NXN:表示 NXN 大小的二维数组:

database[0];       // Related to the first row in the array
database[0][0];    // Is the top left cell in the array.
database[N-1]      // Related to the last array row
database[N-1][N-1] // Is the down right cell in the array.

If you wish to add grade3 into the array for the second student you should insert the grade into:如果您希望将 Grade3 添加到第二个学生的数组中,您应该将成绩插入:

 database[1][2]

Why?为什么? Because the counting in the array is zero based, and hence the second student data is saved in因为数组中的计数是从零开始的,因此第二个学生数据保存在

database[1]  // The second array row.

The first grade should be save in column number 0:一年级应保存在第 0 列中:

database[1][0]

The second grade should be save in column number 1:二年级应保存在第 1 列中:

database[1][1]

The third grade should be save in column number 2:三年级应保存在第 2 列中:

database[1][2]

Here you have a very useful link related to grades 2d-arrays .在这里,您有一个与 Grades 2d-arrays 相关的非常有用的链接

Tip for best practice:最佳实践提示:

When using two-dimensional array it is a good practice to make the numbers in the array as static final names with meaning.使用二维数组时,最好将数组中的数字设置为有意义的静态最终名称。 Instead of:代替:

double [][] database = new double[4][15];

For example you can try the following syntax:例如,您可以尝试以下语法:

private static final Integer NUM_OF_STUDENTS = 4
private static final Integer NUM_OF_TESTS = 15
double [][] database = new double[NUM_OF_STUDENTS][NUM_OF_TESTS];

This is what a 2d array looks like:这是二维数组的样子:

So, according to my understanding, this is how you should declare your 2d array:所以,根据我的理解,这就是你应该如何声明你的二维数组:

double [][] database = new double[15][5];

It means that there are 15 rows (15 students) and 5 columns (1 column to store the name and the other 4 to store the grades of that student).这意味着有 15 行(15 个学生)和 5 列(1 列存储姓名,另外 4 列存储该学生的成绩)。 Now you need a way to store both name and grade in the same array.现在您需要一种将姓名和等级存储在同一个数组中的方法。 So the only you can do this is if you declare the type of the array an Object .因此,您唯一可以这样做的是将数组的类型声明为Object

Object [][] database = new Object[15][5];

An Object can hold any type of data.一个对象可以保存任何类型的数据。 Here is an example:这是一个例子:

database[0][0] = "jon skeet";
database[0][1] = 4.0; // grade in double type... you can also keep it as String
database[0][2] = 4.0;
database[0][3] = 4.0;
database[0][4] = 4.0;

If 2d array is not a requirement then I would suggest you to use a HashMap or something else.如果二维数组不是必需的,那么我建议您使用 HashMap 或其他东西。

Create a Class Student in that class put an Array of Notes, then in the StudenGrade class create an array of Students在该班级创建一个班级学生放置一个笔记数组,然后在 StudenGrade 班级创建一个学生数组

class Student {
 int []grades;
 String name;

 public Student(String name, int NGrades){
 this.name = name
 grades = new int[NGrades];
 }
 public void setGrade(int index, int grade){
 grade[index] = grade;
 }
 public int getGrade(int index){
 return grade[index];
 }
 public String getName(){
 return name;
 }
}

class StudentGrades {
 Student students[];

 public StudentGrades(int NStudents, int NGrades){
 students = new Student[NStudents];
 String names[] = {"Bob","Pete","Rafi","Roff"};
 for(int i=0;i<NStudents;i++){
  students[i] = new Studen(names[i],NGrades);
 }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM