gpt4 book ai didi

java - 即使已处理,ArrayList IndexOutOfBoundsException

转载 作者:行者123 更新时间:2023-11-30 06:18:14 24 4
gpt4 key购买 nike

我有一个方法尝试获取消息数组列表中的当前消息,如果没有则返回 null,但是我得到索引越界异常,我无法理解为什么

public Message getCurrent() {
if(this.size() <= 0) {
return null;
}else {
return this.get(currentMessageIndex);
}

}

下面在另一个类中调用上述方法并抛出异常:

public void run() {
while (running) {
//Message msg = clientQueue.getLast(); // Matches EEEEE in ServerReceiver
Message msg = clientQueue.getCurrent();
System.out.flush();
if (msg != null) {
if (msg.getSent() == false) {
client.println(msg);// Matches FFFFF in ClientReceiver
client.flush();
msg.setSent();

}
}
}
return;
}

最佳答案

public Message getCurrent() {
if(this.size() <= 0) {
return null;
}else {
return (this.size() > currentMessageIndex) ? this.get(currentMessageIndex) : null;
}}

Can you try with this, I have handled fail over case.

关于java - 即使已处理,ArrayList IndexOutOfBoundsException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48772166/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com