【发布时间】:2018-07-24 03:20:54
【问题描述】:
我有一个方法试图在消息数组列表中获取当前消息,如果没有,则返回 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;
}
【问题讨论】:
-
你在扩展一个 ArrayList 吗?如果是这样,我认为您真的应该质疑为什么需要这样做...
-
看起来您在多线程环境中运行它。你把它称为 this.size 是什么意思?
-
在哪里处理,错误信息在哪里?
-
@notionquest消息数组列表的大小
-
看起来您在多线程环境中运行它。除非您在添加/删除和从中获取消息时向我们展示您是如何使用
clientQueue的,否则您只会得到推测性的答案。
标签: java arraylist exception-handling indexoutofboundsexception