java - 如何在不返回值的情况下显示消息
问题描述
在这个函数中,如果 isEmpty ,我不想返回 0 或任何数值..我只想在我从 main 出队时显示消息。我如何通过处理此自定义消息或任何其他方式的异常来做到这一点?
public int dequeue()
{
if(this.isEmpty()){
System.out.println("Queue is Empty !");
return 0;
}
else{
first++;
int x = arr[first];
return x;
}
}
解决方案
public int dequeue() throws ArrayIndexOutOfBoundsException
{
first++;
int x = arr[first];
return x;
}
当你调用它时:
try {
dequeue();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println("Queue is empty!");
}
推荐阅读
- windows - 在 .Appx 包安装结束时触发脚本 [UWP]
- android - 条纹结帐模式键盘涵盖小型设备上的输入和支付按钮
- python - 如何使用异步函数实现 ThreadPool?
- node.js - 使用带有投影和唯一验证的 Mongoose.js 创建文档
- python - 无法使用 Selenium Webdriver 打开超过最大数量的选项卡?
- javascript - AngularJS / Sqlite / Electron - 未知提供者:$dbServiceProvider
- python - 搅拌器:将创建半径锁定到鼠标移动
- python - 异常错误处理扰乱了我的变量
- c# - 如何使用原始媒体基础 API 实时同步视频和音频?
- javascript - 如何在本地状态下从此 API 编辑用户?