如何将我在另一个类中创建的列表数组引用到另一个类中2

时间:2017-04-15 21:39:47

标签: java search arraylist

这是我创建arraylist的地方

MyFunction <- function(x, a=-1, b=0, c=1){
  a + b*x + c*x^2 
}

x <- seq(from = -2, to = 2, length.out = 101)

result <- MyFunction(x)

> result
  [1]  3.0000  2.8416  2.6864  2.5344  2.3856  2.2400  2.0976  1.9584  1.8224  1.6896  1.5600  1.4336  1.3104  1.1904  1.0736
 [16]  0.9600  0.8496  0.7424  0.6384  0.5376  0.4400  0.3456  0.2544  0.1664  0.0816  0.0000 -0.0784 -0.1536 -0.2256 -0.2944
 [31] -0.3600 -0.4224 -0.4816 -0.5376 -0.5904 -0.6400 -0.6864 -0.7296 -0.7696 -0.8064 -0.8400 -0.8704 -0.8976 -0.9216 -0.9424
 [46] -0.9600 -0.9744 -0.9856 -0.9936 -0.9984 -1.0000 -0.9984 -0.9936 -0.9856 -0.9744 -0.9600 -0.9424 -0.9216 -0.8976 -0.8704
 [61] -0.8400 -0.8064 -0.7696 -0.7296 -0.6864 -0.6400 -0.5904 -0.5376 -0.4816 -0.4224 -0.3600 -0.2944 -0.2256 -0.1536 -0.0784
 [76]  0.0000  0.0816  0.1664  0.2544  0.3456  0.4400  0.5376  0.6384  0.7424  0.8496  0.9600  1.0736  1.1904  1.3104  1.4336
 [91]  1.5600  1.6896  1.8224  1.9584  2.0976  2.2400  2.3856  2.5344  2.6864  2.8416  3.0000

plot(result)

这是我的学生班

package sample;


import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Node;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.stage.Stage;
import javafx.scene.control.TextField;
import javafx.scene.control.Button;
import javafx.event.ActionEvent;
import sample.Data.Students;
import java.io.IOException;
import java.util.ArrayList;

public class StudentEnrollment {

    public static ArrayList<Students> SDetails=new ArrayList<Students>();


    public static String StuID="";


    @FXML
    private Button studentenrollmentnextbtn;

    @FXML
    private TextField studentenrollmentname;

    @FXML
    private TextField studentenrollmentid;

    @FXML
    private TextField studentenrollmentage;

    @FXML
    private TextField studentenrollmentnum;

    public void StudentEnroll(ActionEvent m)throws IOException{

        Students s=new Students();
        s.setSname(studentenrollmentname.getText());
        s.setSid(studentenrollmentid.getText());
        StuID=studentenrollmentid.getText();
        s.setSage(Integer.parseInt(studentenrollmentage.getText()));
        s.setSnumber(Integer.parseInt(studentenrollmentnum.getText()));
        SDetails.add(s);


        Parent root= FXMLLoader.load(getClass().getResource("AdminEnterDetails.fxml"));
        Scene scene = new Scene(root);
        Stage stage= (Stage)((Node)m.getSource()).getScene().getWindow();
        stage.setScene(scene);
        stage.show();
    }

}

这是我想要使用arraylist,我想在这里使用它因为,我想检查我的arraylist是否为空,如果它的empyt我想显示错误消息,如果它不是空的我想搜索我在该arraylist中输入的ID

package sample.Data;

public class Students {

    private String Sid;
    private String Sname;
    private int Sage;
    private int Snumber;


    public String getSid() {
        return Sid;
    }

    public void setSid(String sid) {
        Sid = sid;
    }

    public String getSname() {
        return Sname;
    }

    public void setSname(String sname) {
        Sname = sname;
    }

    public int getSage() {
        return Sage;
    }

    public void setSage(int sage) {
        Sage = sage;
    }

    public int getSnumber() {
        return Snumber;
    }

    public void setSnumber(int snumber) {
        Snumber = snumber;
    }

}

2 个答案:

答案 0 :(得分:0)

StudentEnrollment.SDetails.isEmpty();

答案 1 :(得分:0)

你非常接近。 ArrayLists有一个内置的isEmpty()方法,它将返回一个布尔值true / false,具体取决于列表是否为空。

因此,假设您的空if()是您需要逻辑的地方,那么您的ViewbyStudent()方法将如下所示:

public void ViewbyStudent(ActionEvent e) throws IOException {

    if(!studentEnrollment.SDetails.isEmpty()) {
        for (Students m : StudentEnrollment.SDetails)
            if (Objects.equals(m.getSid(), studentuserid.getText())) {

                Parent root = FXMLLoader.load(getClass().getResource("ViewStudentDetails.fxml"));
                Scene scene = new Scene(root);
                Stage stage = (Stage) ((Node) e.getSource()).getScene().getWindow();
                stage.setScene(scene);
                stage.show();

            } else {
                error.setText("Student ID you entered is incorrec,\n or ID couldn't be found,PLEASE TRY AGAIN");
            }
    } else {
        error.setText("Oh no! SDetails is empty!");
    }
}

所以基本上如果列表是 NOT 为空(那是什么!)它将执行你已经拥有的代码块,但现在如果列表 IS 空它只会将文本设置为&#34;哦不! SDetails是空的!&#34;