简单数组队列的创建及演示(Java日记二)

这篇博客展示了一个使用Java实现的简单队列操作类`ArrQueue`,包括添加、获取、显示队首元素和检查队列状态等方法,并在主程序中通过Scanner接收用户输入进行交互操作。在遇到队列满或空的情况时,程序抛出`RuntimeException`,简化了异常处理流程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 命名基本规范,因为是日记不想浪费太多时间在解释说明上。

补充一个小知识点:使用RuntimeException而不是Exception的原因(简单理解就是想少写点代码),在最下方。
import java.util.Scanner;

public class Queue {
    public static void main(String[] args) {
        ArrQueue queue=new ArrQueue(3);
        Scanner scan=new Scanner(System.in);
        boolean loop=true;
        while(loop){
            System.out.print("请输入命令:");
            char key=scan.next().charAt(0);
            switch (key){
                case 'a':
                    try {
                        System.out.print("输入要添加的数:");
                        int n=scan.nextInt();
                        queue.addQueue(n);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'g':
                    try {
                        int elem=queue.getQueue();
                        System.out.println("已取出元素:"+elem);
                    } catch (Exception e) {
                        System.out.println(e.getMessage());

                    }
                    break;
                case 's':
                    try {
                        queue.showQueue();
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'h':
                    try {
                        queue.headQueue();
                    } catch (Exception e) {
                        System.out.println(e.getMessage());
                    }
                    break;
                case 'e':
                    scan.close();
                    loop=false;
                    System.out.println("程序退出!");
                    break;
                default:
                    break;



            }
        }
    }

}
class ArrQueue{
    private int maxsize;
    private int front;
    private int rear;
    private int[] arr;
    public ArrQueue(int maxsize){
        this.maxsize=maxsize;
        arr=new int[maxsize];
        front=-1;
        rear=-1;
    }
    public boolean isFull(){
        return rear==maxsize-1;
    }
    public boolean isEmpty(){
        return front==rear;
    }
    public void addQueue(int n){
        if(isFull()){
            throw new RuntimeException("添加失败,队已满~");
        }
        arr[++rear]=n;

    }
    public int getQueue(){
        if(isEmpty()){
            throw new RuntimeException("取出失败,队已空~");
        }
        return arr[++front];
    }
    public void showQueue(){
        if(isEmpty()){
            throw new RuntimeException("无法显示,队已空~");
        }
        for (int i = front+1; i <=rear ; i++) {
            System.out.println(arr[i]);
        }
    }
    public void headQueue(){
        if(isEmpty()){
            throw new RuntimeException("无法显示,队已空~");
        }
        System.out.println(arr[front+1]);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值