Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java:如何将数组数据传递给方法?_Java_Arrays_Parameter Passing - Fatal编程技术网

Java:如何将数组数据传递给方法?

Java:如何将数组数据传递给方法?,java,arrays,parameter-passing,Java,Arrays,Parameter Passing,我的问题和这两个有点相似 ,及 但是,由于我只需要从一个方法中打印数组的数据,所以我遇到了提供答案的麻烦 这是我的密码: public static void main(String[] args) { Student[] s1 = {new Student("Karen", "male", 121, "N", 2, "e"), new Student("K&quo

我的问题和这两个有点相似

  • ,及
  • 但是,由于我只需要从一个方法中打印数组的数据,所以我遇到了提供答案的麻烦

    这是我的密码:

        public static void main(String[] args) {
        Student[] s1 = {new Student("Karen", "male", 121, "N", 2, "e"),
                new Student("K", "male", 122, "e", 3, "e"),
                new Student("L", "male", 123, "", 4, ""),
                new Student("M", "male", 124, "", 5, ""),
                new Student("O", "female", 125, "", 1, "")};
    
        s1.studentInfo(); //My questions involves something like this
    
    }
    
    这里的设置是这样的

    new Student(String StudentName, String Gender, int StudentNumber, String program, int YearLevel, String Orgs)
    
    这是一种方法,我希望数组数据可以访问,然后打印出来

        public void studentInfo(){
        System.out.println("Student name: "+super.getName());
        System.out.println("Gender: "+super.getGender());
        System.out.println("Student number: "+super.getStudNumber());
        
        System.out.println("Program: "+getProgram());
        System.out.println("Year Level: "+getYrlevel());
        System.out.println("Organization: "+getOrg());
    }
    

    看起来你想要的是这样的:

    private static void studentInfo(final Student[] students) {
        for (final Student student : students) {
            System.out.println("Student name: "+ student.getName());
            System.out.println("Gender: "+ student.getGender());
            System.out.println("Student number: "+ student.getStudNumber());
    
            System.out.println("Program: "+ student.getProgram());
            System.out.println("Year Level: "+ student.getYrlevel());
            System.out.println("Organization: "+ student.getOrg());
        }
    }
    
    studentInfo(s1);
    
    你可以这样称呼它:

    private static void studentInfo(final Student[] students) {
        for (final Student student : students) {
            System.out.println("Student name: "+ student.getName());
            System.out.println("Gender: "+ student.getGender());
            System.out.println("Student number: "+ student.getStudNumber());
    
            System.out.println("Program: "+ student.getProgram());
            System.out.println("Year Level: "+ student.getYrlevel());
            System.out.println("Organization: "+ student.getOrg());
        }
    }
    
    studentInfo(s1);
    

    另请注意,参数名称应以小写字母开头,如下所示。

    只需在数组中迭代,并将元素传递给
    studentInfo
    方法:

    publicstaticvoidmain(字符串[]args){
    学生[]s1={新生(“凯伦”,“男”,121,“N”,2,“e”),
    新生(“K”,“男”,122,“e”,3,“e”),
    新生(“L”,“男”,123”,“4,”,
    新生(“M”,“男”,124”,“5,”,
    新生(“O”,“女”,125“,1,”)};
    学生(s:s1){
    学生资讯(s);;
    }
    }
    公共静态无效学生信息(学生s){
    System.out.println(“学生名:+s.getName());
    System.out.println(“性别:+s.getGender());
    System.out.println(“学生编号:+s.getStudNumber());
    System.out.println(“程序:+s.getProgram());
    System.out.println(“年度级别:+s.getYrlevel());
    System.out.println(“组织:+s.getOrg());
    }
    
    首先定义如下方法

    public void studentInfo(){
        System.out.println("Student name: "+this.getName());
        System.out.println("Gender: "+this.getGender());
        System.out.println("Student number: "+this.getStudNumber());
        
        System.out.println("Program: "+this.getProgram());
        System.out.println("Year Level: "+this.getYrlevel());
        System.out.println("Organization: "+this.getOrg());
    }
    
    在学生课堂上。然后,您可以在任何需要的地方使用:

    Stream.of(s1).forEach(Student::studentInfo)
    

    其中s1是您的students数组。

    s1是一个数组,它不能有studentInfo()方法。您必须在数组上循环,然后调用一个方法来打印数据。我尝试了
    s1.studentInfo之前,但我只收到一个错误,表示无法调用数组Student[]上的studentInfo(Student),IntelliSense表示它必须是
    s1。长度
    。啊,抱歉,输入错误。我本来应该是
    studentInfo(s)
    而不是
    s1
    我认为你之前的想法要好得多:创建一个
    Student\studentInfo()
    并从循环中调用它。当然,更好的名称应该是
    printStudentInfo()
    。我认为这是个人偏好,也取决于用例。如果只在一个地方调用该方法,则不会有太大区别。这个答案也适用于静态方法,谢谢。