java实现磁盘先来先服务算法

package demo;

import java.awt.List;
import java.util.ArrayList;
import java.util.Arrays;

public class DemoClass {
    //定义成员变量
    private int distance=0;
    private int[] list;
    private int init_number;
    DemoClass(int init_number, int[] list) {
        this.init_number = init_number;
        this.list = list;
    }
    public void fcfs() {
        for(int i : this.list) {
            if (i>this.init_number) {
                this.distance = this.distance+(i-this.init_number);
                this.init_number = i;
            }else {
                this.distance = this.distance+(this.init_number-i);
                this.init_number = i;
            }
        }
        System.out.print("先来先服务算法距离:"+this.distance);
    }
    public void sttw() {
        ArrayList<Integer> arr = new ArrayList<Integer>();
        for(int i: this.list) {
            int c = i-this.init_number;
            if (c<0) {
                c = -c;
            }
            arr.add(c);
        }
//        Object[] ar;
//        ar = arr.toArray();
//        Arrays.sort(ar);
//        for(Object i : ar) {
//            System.out.print(i);
//        }
//        int cha =(int) ar[0]-this.init_number;
//        this.distance = this.distance+(Math.abs(cha));
//        System.out.print("此次距离"+this.distance);
    }
    public static void main(String[] args) {
        int[] list = {1,2,34,5};
        DemoClass demo = new DemoClass(4,list);
        demo.sttw();
    }

}

猜你喜欢

转载自www.cnblogs.com/szj666/p/12066060.html