数组大小声明

时间:2016-11-08 14:00:16

标签: java arrays

我已在下面发布了我的代码。我在声明数组wrongAnswers的行上遇到了问题。我以前能够让我的代码工作,但问题是有些人自己删除了我的所有文件。我可以在不使用ListArrayList的情况下使其正常运行。我只是想了解在尝试使用其他方法之前我现在能够如何工作。我知道Java数组是不可变的。但是,我仍然能够以某种方式让它在以前工作。如果有人能帮助我弄清楚我以前做过什么,我会非常感激。

private Scanner keyboard = new Scanner(System.in);

private final String[] testAnswers = {
        "B","D","A","A","C",
        "A","B","A","C","D",
        "B","C","D","A","D",
        "C","C","B","D","A"};
private String[] studentAnswers = new String[20];
/*
private String[] studentAnswers = {
        "B","D","A","A","C",
        "A","B","A","C","D",
        "B","C","D","A","D",
        "C","C","B","D","A"};
*/
private int[] wrongAnswers;
private int answeredCorrectly = 0;

public void getStudentAnswers() {
    for(int x = 0; x < 20; x++) {
        do {
            System.out.print("Enter answer for #" + (x + 1) + " : ");
            this.studentAnswers[x] = keyboard.next().toUpperCase();
            if (!"A".equals(this.studentAnswers[x]) && 
                    !"B".equals(this.studentAnswers[x]) &&
                    !"C".equals(this.studentAnswers[x]) && 
                    !"D".equals(this.studentAnswers[x])) {
                System.out.println("Invalid input.");
            }
        } while(!"A".equals(this.studentAnswers[x]) && 
                !"B".equals(this.studentAnswers[x]) &&
                !"C".equals(this.studentAnswers[x]) && 
                !"D".equals(this.studentAnswers[x]));
    }
}

public int totalCorrect() {
    int arrayLocation = 0;

    for(int x = 0; x < 20; x++) {
        if (this.studentAnswers[x].equals(this.testAnswers[x])) 
            this.answeredCorrectly++;
        else 
            this.wrongAnswers[arrayLocation++] = x;
    }

    return this.answeredCorrectly;
}

public int totalIncorrect() {
    return 20 - this.answeredCorrectly;
}

public boolean passed() {
    return this.answeredCorrectly >= 15;
}

public void questionsMissed() {
    if(this.answeredCorrectly != 20) {
            for(int x = 0; x < this.wrongAnswers.length; x++) {
            System.out.println(this.wrongAnswers[x]);
        }
    }
}

2 个答案:

答案 0 :(得分:1)

如果代码写得很好,节省空间(这是你想要做的)通常会降低性能,反之亦然。你可以实现自己想要的目标,但是你会失去性能,正如你所看到的那样。

我发现演绎在解决类似问题时很有用。条件:

1)数组是不可变的 2)您想要分配所需的确切空间量

第2点提出了一个问题:你怎么知道你需要多少空间?明显的答案:知道你有多少(正确)答案。从那里你可以做到:

public int totalCorrect() {
    for(int x = 0; x < 20; x++) {
        if (this.studentAnswers[x].equals(this.testAnswers[x])) 
            this.answeredCorrectly++;
    }

    this.wrongAnswers = int[20 - this.answeredCorrectly];

    // Here you want to create your wrongAnswers, but you have to go over 
    // the same array twice...
    int arrayLocation = 0;
    for(int x = 0; x < 20; x++) {
        if (!this.studentAnswers[x].equals(this.testAnswers[x])) 
            this.wrongAnswers[arrayLocation++] = x;
    }


    return this.answeredCorrectly;
}

可能有更多方法可以做类似的事情并获得更好的表现。乍一看,他们看起来像是糟糕的方法,我会使用List,就像已经提出的那样,或者也许是Set,但谁知道......

答案 1 :(得分:0)

private int [] wrongAnswers = new int [20];