[java]代码库/**
* 浏览器浏览历史记录实现
*
* url栈
*/
public class SequenceStack {
private String[] urls; // 数组,存储用户请求的url
private int top; // 栈顶指针
private final static int maxSize = 5; // 数组长度,暂定5
public SequenceStack() { // 初始化
urls = new String[maxSize];
top = -1;
}
// 最新url入栈
public void push(String url) {
if (top == maxSize - 1) { // 栈满处理
if (url.equals(urls[top])) {// 防止刷新,如果栈顶url与传入的url相等,即为刷新操作
return;
}
// 更新数组保存的url
String[] otherUrls = urls.clone();
for (int i = 0; i <= maxSize - 1; i++) {
if (i == top) {
break;
}
otherUrls[i] = urls[i + 1];// 前移
}
otherUrls[top] = url;
urls = otherUrls;
} else { // 栈未满
top++;
urls[top] = url;
}
}
// 栈顶元素即为前页的url
public String pop() {
if (top == -1 || top == 0) {
return null;
}
return urls[top];
}
public String[] getUrls() {
return urls;
}
public void setUrls(String[] urls) {
this.urls = urls;
}
public int getTop() {
return top;
}
public void setTop(int top) {
this.top = top;
}
public static int getMaxsize() {
return maxSize;
}
}