【问题标题】:Java return arrayJava返回数组
【发布时间】:2013-10-27 10:32:16
【问题描述】:

我正在开发一个基于特定模块返回学生分数的简单应用程序。我的主要问题是getModuleMark 方法,因为我需要它来返回给定模块索引的模块标记。

对于setModuleMark 方法,我已经传递了索引模块和标记参数。我只是有点困惑,我需要在模块标记的返回中添加什么。

在我运行应用程序的那一刻,我得到以下输出:

乔博客

模块 0:50.0

模块 1:50.0

模块 7:50.0

见下面的代码:

public class Student {

    public static void main(String[] args) {

     Student student = new Student("Joe", "Bloggs"); 
   
    // Add some marks to the student. 
    student.setModuleMark(0, 10);   
    student.setModuleMark(1, 80);  
    student.setModuleMark(7, 50);
  
   
    // Display the marks.
    System.out.println(student.getForename() + " " +     student.getSurname());
    System.out.println("Module 0: " + student.getModuleMark(0));
    System.out.println("Module 1: " + student.getModuleMark(1));
    System.out.println("Module 7: " + student.getModuleMark(7));
      } 
    
  

    private String forename;
    private String surname;
    private double marks;

    public Student(String forename, String surname) {
      super();
      this.forename = forename;
      this.surname = surname; 
    
      double [] ModuleMark = new double [7]; //Creates array of fixed size 7
    }
  
    /**
     * @return the forename
     */
     public String getForename() { 
        return this.forename;
     }
  
     /**
      * @return the surname
      */
     public String getSurname() {
       return this.surname;
     }
  
     /**
      * @param marks the marks to set
      * @param i 
      */     
     public double getModuleMark (int in) {
       return this.marks;        
     }
  
     public void setModuleMark(int in, double marks) {
     
    this.marks = marks;
  }
}

【问题讨论】:

    标签: java arrays return


    【解决方案1】:

    似乎有很多事情是错误的。

    首先,ModuleMark 应该在类中声明,而不是在构造函数中。

    private double[] ModuleMark; // Declare here
    
    public MyMain(String forename, String surname) {
        this.forename = forename;
        this.surname = surname;
        this.ModuleMark = new double[7]; // Creates array of fixed size 7
    }
    

    接下来,你的 getModuleMarks 和 setModuleMarks 方法需要是这样的

    public double getModuleMark(int in) {
        return this.ModuleMark[in]; // return the value present at the given index
    }
    
    public void setModuleMark(int in, double marks) {
        this.ModuleMark[in] = marks; // set the marks at the given index in the array.
    }
    

    另外,由于ModuleMark 是一个大小为 7 的数组,您不能使用 索引 7。它会抛出一个ArrayIndexOutOfBoundsException,因为在一个数组中,最大可能的可访问索引总是array.length - 1

    student.setModuleMark(6, 50); // The max possible index
    ...
    System.out.println("Module 7: " + student.getModuleMark(6)); // The max possible index
    

    注意:进行这些更改后,private double marks; 将不再使用。如果您将来打算将其用于其他目的,您可以丢弃它或拥有它。

    【讨论】:

      【解决方案2】:

      公开课学生{

      public static void main(String[] args) {
      
       Student student = new Student("Joe", "Bloggs"); 
      
      // Add some marks to the student. 
      student.setModuleMark(0, 10);   
      student.setModuleMark(1, 80);  
      student.setModuleMark(7, 50);
      
      
      // Display the marks.
      System.out.println(student.getForename() + " " +     student.getSurname());
      System.out.println("Module 0: " + student.getModuleMark(0));
      System.out.println("Module 1: " + student.getModuleMark(1));
      System.out.println("Module 7: " + student.getModuleMark(7));
        } 
      
      
      
      private String forename;
      private String surname;
      private double[] marks;
      
      public Student(String forename, String surname) {
        super();
        this.forename = forename;
        this.surname = surname; 
      
        marks = new double [8]; //Creates array of fixed size 7
      }
      
      /**
       * @return the forename
       */
       public String getForename() { 
          return this.forename;
       }
      
       /**
        * @return the surname
        */
       public String getSurname() {
         return this.surname;
       }
      
       /**
        * @param marks the marks to set
        * @param i 
        */     
       public double getModuleMark (int in) {
         return this.marks[in];        
       }
      
       public void setModuleMark(int in, double marks) {
      
      this.marks[in] = marks;
      

      } }

      【讨论】:

      • 检查这个............首先你已经创建了 7 个元素的数组,但是你试图设置并获取第 8 个元素值,它将显示索引越界错误。检查这个我修改了你的代码。
      【解决方案3】:

      就我而言,你必须先添加

      private double [] ModuleMark

      作为学生类字段,然后像

      public double getModuleMark (int in){
         return this.ModuleMark[in];
        }
      

      【讨论】:

        【解决方案4】:

        由于您的学生可以有许多模块,每个模块都有自己的标记,那么基本上一个数组适合您的用例。所以你需要删除double marks,只保留数组double [] ModuleMark

        你可以像这样实现你的getter和setter:

        public double getModuleMark (int in) {
            return ModuleMark[in];
        }
        
        public void setModuleMark(int in, double mark) {
            ModuleMark[in] = mark;
        }
        

        请注意 ArrayIndexOutOfBoundsException 。更好的是,我建议使用List 女巫可以自动增长,它还能够使用(从零开始的)索引访问元素。

        还请考虑成员和类的 java 命名约定。

        【讨论】:

          【解决方案5】:

          按照惯例,第一个 ModuleMark 应该是 moduleMark。 :-)

          对于 moduleMarks,第二次使用 getter 和 Setter 应该是这样(按照惯例)。

           public double[] getModuleMark() {
          return moduleMark;
           }
           public void setModuleMark(double[] moduleMark) {
          this.moduleMark = moduleMark;
           }
          

          第三——模块标记的范围可能是这样的:(不仅仅在学生构造函数的范围内定义)

             public class Student {
                private double [] moduleMark = new double [7]; 
              ....
          

          那么,如果我认为您正在尝试这样做:

              // Add some marks to the student.
              student.setSpecificModuleMark(0, 10);
              student.setSpecificModuleMark(1, 80);
              student.setSpecificModuleMark(6, 50);
          
              // Display the marks.
              System.out.println(student.getForename() + " " + student.getSurname());
              System.out.println("Module 0: " + student.getSpecificModuleMark(0));
              System.out.println("Module 1: " + student.getSpecificModuleMark(1));
              System.out.println("Module 6: " + student.getSpecificModuleMark(6));
             }
          
          private double getSpecificModuleMark(int i) {
              // TODO Auto-generated method stub
              return this.moduleMark[i];
             }
          
          private void setSpecificModuleMark(int i, int j) {
              this.moduleMark[i] = j;
          
             }
          

          第四:注意您尝试打印出索引超出范围的 7.. 所以使用 0--6。

          【讨论】:

            猜你喜欢
            • 2011-03-17
            • 1970-01-01
            • 1970-01-01
            • 2012-01-13
            • 2016-07-10
            • 2014-12-14
            • 1970-01-01
            • 1970-01-01
            • 2011-08-21
            相关资源
            最近更新 更多