我試圖偷看我的隊列并抓住第一個條目,這樣我就可以按下刪除按鈕來擺脫它,但是偷看顯示的是最后輸入的而不是第一個。我已經嘗試過 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
添加回答
舉報
0/150
提交
取消