亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Java Queue Peek() 保持返回最新條目而不是第一次輸入

Java Queue Peek() 保持返回最新條目而不是第一次輸入

斯蒂芬大帝 2022-01-19 17:00:18
我試圖偷看我的隊列并抓住第一個條目,這樣我就可以按下刪除按鈕來擺脫它,但是偷看顯示的是最后輸入的而不是第一個。我已經嘗試過 peek 和 peek front 。private int maxSize;private String[] queArray;private int front;private int rear;private int nItems;public String FN,LN,PN,Email,Addres,State,Zip,LicensePlate;public Queue(String fN, String lN, String pN, String email, String address, String state, String zip,        String licensePlate) {    maxSize++;    queArray = new String[maxSize];    front = 0;    rear = -1;    nItems = 0;    FN = fN;    LN = lN;    PN = pN;    Email = email;    Addres = address;    State = state;    Zip = zip;    LicensePlate = licensePlate;}public void insert(String FN, String LN, String PN, String Email, String Addres, String State, String Zip,        String LicensePlate) {    String input = "{" + "First Name: "+ FN + ", " +"  Last Name: "+ LN +", "+"   Phone Number: "+ PN + ", " +"  Email: "+ Email +", " +"  Address: "+ Addres + ", " +"  State: "+  State +", "+"  Zip: "+  Zip + ", " +"  LicensePlate: "+ LicensePlate + "}";    if (rear == maxSize - 1)        rear = -1;    queArray[++rear] = input;    nItems++;    }  public String peekFront() {    return queArray[front++];}public String peek() {    return queArray[front];}通過將 maxSize++ 更改為 maxSize = 5 來修復
查看完整描述

1 回答

?
溫溫醬

TA貢獻1752條經驗 獲得超4個贊

如果我沒記錯的話,這是由于您的 maxSize 機制而發生的。


maxSize 永遠不會設置為一個數字,只有一次增加maxSize++. 所以 maxSize 始終為 1。


現在當已經插入一個元素時,將調用以下代碼,因為后部現在為 0 并且 maxSize-1=1-1。


if (rear == maxSize - 1)

        rear = -1;

    queArray[++rear] = input;


您只是覆蓋了數組中唯一的元素。


與其編寫自己的基于數組的隊列來保存數據,不如考慮為數據使用自定義對象和已經實現的隊列,如java.util.Queue


查看完整回答
反對 回復 2022-01-19
  • 1 回答
  • 0 關注
  • 172 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號