📄 optimal.java
字号:
/* * To change this template, choose Tools | Templates * and open the template in the editor. */package util;import java.awt.Color;import java.awt.Label;import java.util.logging.Level;import java.util.logging.Logger;import javax.swing.JLabel;import javax.swing.JProgressBar;/** * * @author Administrator */public class Optimal extends PageReplace{ int[] seq=null; Label[] label=null; int []onLabel=null; int in; //换进来页面 int still;//原来就有又进来的 double []efficiency=null; //换页率 double formFeed=0; //换页次数 String nextSeq = null; String lastSeq = null; Label next = null; Label last = null; Label efficiencyLabel = null; public Optimal(int[]s,double[] eff,Label[] l,Label next,Label last,Label efficiencyLabel) { seq=s; label=new Label[l.length]; onLabel=new int[l.length]; efficiency= eff; this.next=next; this.last=last; this.efficiencyLabel = efficiencyLabel; for (int i = 0; i < l.length; i++) { label[i]= l[i]; onLabel[i]=0; } char[] show = new char[40]; for (int i = 0,j=0; i < seq.length; i++) { show[j++] = (char)(seq[i]+48); show[j++] = ' '; } nextSeq = new String().valueOf(show); lastSeq = ""; } public void run() { next.setVisible(true); next.setText("待访问的页面编号为:"+nextSeq); last.setVisible(true); last.setText("已访问的页面编号为:"+lastSeq); efficiencyLabel.setVisible(true); int page=0; for (int i = 0; i < seq.length; i++) { try { in = -1; still = -1; if (page < label.length) { for (int j = 0; j < page; j++) { if (seq[i] == onLabel[j]) { still = j; } } if (still == -1) { formFeed++; in = page; onLabel[page] = seq[i]; page++; } } else { for (int j = 0; j < onLabel.length; j++) { if (seq[i] == onLabel[j]) { still = j; } } if (still == -1) { formFeed++; in = findOptimal(i); onLabel[in] = seq[i]; } } efficiency[i] = (double) (formFeed/(i+1)); show(i); Thread.sleep(1000); } catch (Exception ex) {} } } private int findOptimal(int cur) { boolean []isOpti=new boolean[label.length]; for (int i = 0; i < isOpti.length; i++) isOpti[i]=true; int t=0; for (int i = cur; i < seq.length; i++) { for (int j = 0; j < onLabel.length; j++) { if(seq[i]==onLabel[j]) { isOpti[j]=false; t++; } } if(t==label.length-1) break; } for (int i = 0; i < isOpti.length; i++) { if( isOpti[i]==true) return i; } return -1; } private void show(int cur) { for (int i = 0; i < label.length; i++) { label[i].setText(Integer.toString(onLabel[i])); label[i].setBackground(Color.CYAN); } if(still!=-1) label[still].setBackground(Color.green); else label[in].setBackground(Color.red); lastSeq = lastSeq.concat(nextSeq.substring(0, 2)); nextSeq = nextSeq.substring(2); next.setText("待访问的页面编号为:"+nextSeq); last.setText("已访问的页面编号为:"+lastSeq); efficiencyLabel.setText("当前缺页率为:"+Double.toString(efficiency[cur])); } public static void main(String args[]) { int []s={7,2,1,2,3,3,6,4,2,3,7,3,2,1,2,9,1,7,5,1}; Label []l=new Label[3]; // Optimal opti=new Optimal(s,l); // opti.tkOptimal(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -