我试图偷看我的队列并抓住第一个条目,这样我就可以按下删除按钮来摆脱它,但是偷看显示的是最后输入的而不是第一个。我已经尝试过 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
提交
取消