如何解决越界异常错误?

发布于 2024-12-19 14:28:32 字数 1347 浏览 2 评论 0原文

我昨天发布了一个关于我正在编写的一些代码的问题,并且在使类正确实现接口时遇到了困难。感谢这里用户的大力帮助,该问题已得到修复,因此我想我可能会再次获得帮助。

我现在遇到了越界异常错误的问题,并且在我的一生中,我看不出问题是什么,尽管我对 java 很陌生。

该应用程序显示钢琴键,允许用户单击它们,并保存单击的顺序以及特定声音的音符的 midicode。然后,当用户单击播放时,它会按照保存音符的顺序调用曲调。我使用数组列表来存储笔记,然后希望在用户单击“播放”时调用它们,该方法调用“start”方法,然后重复调用“getNextNote”方法,直到没有更多笔记。但是,每当我单击“播放”时,我都会收到越界异常错误。下面是我到目前为止的代码;

import java.util.*;
import mvcchords.*;

public class MyNoteStore implements NoteStore {

public ArrayList<Integer> Notes = new ArrayList<Integer>();
int k;

public void noteAdded(int midicode) {
    Notes.add(midicode);
}

public boolean hasNextNote() {
    if(Notes.get(k) != null)
        return true;
    else
        return false;
}

public int getNextNote() {

        if(hasNextNote() == true)
            return Notes.get(k);
        else
            return 0;
    }

public void start(int sortOrder) {

    for(k = 0; k < Notes.size(); k++){
    hasNextNote();
    getNextNote();
    }
  }
}

以下是当我尝试单击“播放”并存储 3 个音符时出现的错误(尽管无论我存储了多少个音符,错误都是相同的,只是末尾的数字等于我选择的音符数量):

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3

任何对此的帮助会很好,提前谢谢您:)

我也会尽力提供所需的任何额外信息:)

编辑:将for循环更改为

 for(k = 0; k < Notes.size(); k++)

并且仍然发生相同的错误:(

I posted a question yesterday about some code I was writing and was having trouble making a class implement an interface correctly. Thanks to the great help of users on here that has been fixed, and so I thought I could possibly get help again.

I am now having trouble with an out of bounds exception error and, for the life of me, I can't see what the problem is, though I am quite new to java.

The application displays piano keys which allow the user to click on them, and it saves the order they were clicked and the midicode of the notes for the specific sound. Then when the user clicks play, it recalls the tune in the order the notes were saved. I am using an array list to store the notes and then want to recall them when the user clicks play, which calls the "start" method and then repeatedly calls a "getNextNote" method until there are no more notes. However, whenever I click play I get an out of bounds exception error. Below is my code so far;

import java.util.*;
import mvcchords.*;

public class MyNoteStore implements NoteStore {

public ArrayList<Integer> Notes = new ArrayList<Integer>();
int k;

public void noteAdded(int midicode) {
    Notes.add(midicode);
}

public boolean hasNextNote() {
    if(Notes.get(k) != null)
        return true;
    else
        return false;
}

public int getNextNote() {

        if(hasNextNote() == true)
            return Notes.get(k);
        else
            return 0;
    }

public void start(int sortOrder) {

    for(k = 0; k < Notes.size(); k++){
    hasNextNote();
    getNextNote();
    }
  }
}

Below is the error I get when I try and click play with 3 notes stored (though the error is the same no matter how many notes I have stored, just with the numbers at the end equal to how many notes I have selected):

Exception in thread "AWT-EventQueue-0" java.lang.IndexOutOfBoundsException: Index: 3, Size: 3

Any help with this would be great, thank you in advance :)

I will also do my best to provide any extra information required :)

EDIT: Changed the for loop to

 for(k = 0; k < Notes.size(); k++)

And the same error still occurs :(

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

最佳男配角 2024-12-26 14:28:32

您正在检查 vs. <= size(),而不是 < size(),这意味着如果 size 为 2,您将查找索引 2(而有效索引为 0 和 1)。

You're checking vs. <= size(), instead of < size(), which means if size is 2, you'll look up index 2 (while the valid indicies are 0 and 1).

不如归去 2024-12-26 14:28:32

将您的代码替换为以下代码:

public class MyNoteStore implements NoteStore {

private List<Integer> notes = new ArrayList<Integer>();

public void noteAdded(int midicode) {
    notes.add(midicode);
}

public boolean hasNextNote() {
   // do you need this method?
}

public int getNextNote() {
   // do you need this method?
}
public void start(int sortOrder) {
    for(Integer note : notes){
       // "note" var contains each note from notes
    }
  }
}

Replace your code with code below:

public class MyNoteStore implements NoteStore {

private List<Integer> notes = new ArrayList<Integer>();

public void noteAdded(int midicode) {
    notes.add(midicode);
}

public boolean hasNextNote() {
   // do you need this method?
}

public int getNextNote() {
   // do you need this method?
}
public void start(int sortOrder) {
    for(Integer note : notes){
       // "note" var contains each note from notes
    }
  }
}
似狗非友 2024-12-26 14:28:32

您从收藏中读取了过多的一项:

for(k = 0; k <= Notes.size(); k++) ...

比较应显示为 k <改为 Notes.size();。该集合的索引为 0,因此最后一个条目的索引为 size-1,而不是 size

You read one item too much from your collection:

for(k = 0; k <= Notes.size(); k++) ...

The comparison should read k < Notes.size(); instead. The collection is 0-indexed and therefore the last entry has index size-1 and not size.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文