Java如何在方法中引用数组而不作为参数传递

时间:2016-09-02 09:24:42

标签: java arrays

我正在完成一项任务,我不确定是否可以在此处发布我的代码,但我会尽力解释我的问题。我必须编写一个类,它接受来自单独程序的学生答案数组,并将该数组与我正在编写的类中初始化的一组正确答案进行比较。然后,该类通过方法计算正确和错误答案的数量,然后将这些方法发送回程序以输出结果。我遇到麻烦的部分是我必须制作的方法,它创建一个数组,存储学生出错的问题的问题编号。我用for循环来比较构造函数中的两个数组并初始化一个新数组来存储与错误响应相关的问题数,通常我只是将数组作为方法头中的参数传递,但程序是class works with将方法定义为没有参数,我也不知道将数组从构造函数传递给方法的任何其他方法。对不起,如果这个问题听起来很愚蠢,那可能是因为我一直在研究这个问题的时间超过了我想承认的时间。基本上我只是想知道是否有任何方法可以从方法访问构造函数中初始化的数组而不作为参数传递?

public class Example {
    private char[] correctAnswers;
    private char[] studentAnswers;
    private int count = 0;

    // Constructor accepts an array of answers from the other program.
    public Example(char[] answers) {
        char[] correctAnswers = {'a', 'b', 'c'};
        int question_number = 1;
        int[] missed = new int[3];
        // Copy answers array to studentAnswers array
           *Insert code here 

       //Using a for loop I Compare the two arrays studentAnswers and 
       //correctAnswers and increment variables count and question_number
       //and if a question is wrong I populate the missed array with 
       // the question_number
          *Insert code here*

       public int totalCorrect() {
           return count;
           }

       public int[] totalIncorrect() {
           incorrect = 3 - count;
           return incorrect;
           }

       // This method can't accept any arguments
       public int[] questionsMissed() {
           return missed; // How do I access this array which is 
                          // initialised in the constructor?
           } 

2 个答案:

答案 0 :(得分:0)

在某些方面,您仍然需要使用它来使用它的方法。

  • 您可以公开显示该对象,(不推荐)
  • 使用命令或构建器模式。实际上这仍然是作为参数添加它,但你可以有一个空的cTor,并在你完成之后调用build / execute之前添加它。我认为这会解决你的问题。

答案 1 :(得分:0)

我在下面的代码的评论中添加了一些建议,您应该能够访问missed,如下所示,但如果您要实例化课程Example,那么missed数组也将被初始化,所以你不应该在这里得到任何NPE。

另请注意,missed = new int[3];会产生类似[0,0,0]的数组,直到您为其指定一些值。

public class Example {

    private char[] correctAnswers;
    private char[] studentAnswers;

    private int[] missed; // here you're defining the array, this can be used throughout the class.

    private int count = 0;

    // Constructor accepts an array of answers from the other program.
    public Example(char[] answers) {
        correctAnswers = new char[]{'a', 'b', 'c'}; // Here, don't call char[] again because you've already defined this array as char[] earlier
        int question_number = 1;
        missed = new int[0]; // here you are initialising the array with 3 elements

        // Copy answers array to studentAnswers array
        *Insert code here 

        //Using a for loop I Compare the two arrays studentAnswers and 
        //correctAnswers and increment variables count and question_number
        //and if a question is wrong I populate the missed array with 
        // the question_number
        *Insert code here*

        for(int i=0; i<studentAnswers.length(); i++){

            if(studentAnswers[i] != correctAnswers[i]){
                missed = addElement(missed, i); // Adding an element to the list, this isn't a good way to do it
            }

        }

    }

    public int totalCorrect() {
        return count;
    }

    public int[] totalIncorrect() {
        incorrect = 3 - count;
        return incorrect;
    }

    // This method can't accept any arguments
    public int[] questionsMissed() {
        try {
            return missed;  // You can access this, and return only if it has been initialised before accessing this method.
        } catch(NullPointerException e){
            System.out.println("the array wasn't initialised before accessing this method")
        }
    }

    /**
      * This method just takes the current array, makes a new one and adds an element to it, it's mocking Collections.add(E e) 
    */
    static int[] addElement(int[] a, int e) {
        a  = Arrays.copyOf(a, a.length + 1);
        a[a.length - 1] = e;
        return a;
    }

}

编辑:我已经添加了一些关于如何实现你想要做的事情的建议但是在这里使用数组似乎是一个非常糟糕的选择。我认为你最好使用Collections。因为missed不需要使用3的长度进行初始化,除非所有3答案都是错误的。