您的位置:首页 > 编程语言 > Java开发

内存动态分配的首先适应、最优适应、最坏适应算法的实现(java 版)

2007-05-07 00:21 886 查看
public class DynamicAssign {

 public static void main(String[] args) {
  Area[] area = new Area[5];
  area[0] = new Area(0, 20);
  area[1] = new Area(1, 30);
  area[2] = new Area(2, 40);
  area[3] = new Area(3, 50);
  area[4] = new Area(4, 150);
  Memory[] memory = new Memory[4];
  memory[0] = new Memory(0, 100);
  memory[1] = new Memory(1, 30);
  memory[2] = new Memory(2, 50);
  memory[3] = new Memory(3, 40);
  FirstAdapt(memory, area);    
  BestAdapt(memory, area);   
  BadAdapt(memory,area);
 }

//首先适应算法

 public static void FirstAdapt(Memory[] memory, Area[] area) {
  int k;
  for (int i = 0; i < memory.length; i++) {
   k = area.length;
   for (int j = 0; j < area.length; j++) {
    if ((memory[i].getValue()) <= (area[j].getValue())) {
     area[j].setValue(area[j].getValue() - memory[i].getValue());
     System.out.println(memory[i] + "--->" + area[j]);
     break;
    } else
     k--;
    if (k < 1) {
     System.out.println(memory[i] + " Failed !");
    }

   }

  }

 }

//最优适应算法

 public static void BestAdapt(Memory[] memory, Area[] area) {
  int i, j;
  for (i = 0; i < memory.length; i++) {
   boolean flag = true;

   int min = 0;
   for (j = 0; j < area.length; j++) {
    if (flag) {
     if (memory[i].getValue() <= area[j].getValue()) {
      min = area[j].getValue();
      flag = false;
     }
    } else if ((memory[i].getValue() <= area[j].getValue())
      && (min >= area[j].getValue())) {
     min = area[j].getValue();
    }
   }
   if (min == 0) {
    System.out.println(memory[i] + " Failed !");

   } else {
    for (j = 0; j < area.length; j++) {
     if (area[j].getValue() == min) {
      System.out.println(memory[i] + "--->" + area[j]);
      area[j].setValue(min - memory[i].getValue());
      break;
     }
    }
   }

  }

 }

//最坏适应算法

 public static void BadAdapt(Memory[] memory, Area[] area) {
  int i, j;
  for (i = 0; i < memory.length; i++) {
   boolean flag = true;

   int max = 0;
   for (j = 0; j < area.length; j++) {
    if (flag) {
     if (memory[i].getValue() <= area[j].getValue()) {
      max = area[j].getValue();
      flag = false;
     }
    } else if ((memory[i].getValue() <= area[j].getValue())
      && (max <= area[j].getValue())) {
     max = area[j].getValue();
    }
   }
   if (max == 0) {
    System.out.println(memory[i] + " Failed !");

   } else {
    for (j = 0; j < area.length; j++) {
     if (area[j].getValue() == max) {
      System.out.println(memory[i] + "--->" + area[j]);
      area[j].setValue(max - memory[i].getValue());
      break;
     }
    }
   }

  }
 }
}

//定义一个内存作业区

class Area {

 private int id;

 private int value;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public int getValue() {
  return value;
 }

 public void setValue(int value) {
  this.value = value;
 }

 Area(int id) {
  this.id = id;
 }

 Area(int id, int value) {
  this.id = id;
  this.value = value;
 }

 public String toString() {
  return "Area:" + id;

 }

}

//定义一个内存作业

class Memory {

 Area[] area = new Area[5];

 Memory[] memory = new Memory[4];

 private int id;

 private int value;

 public int getId() {
  return id;
 }

 public void setId(int id) {
  this.id = id;
 }

 public int getValue() {
  return value;
 }

 public void setValue(int value) {
  this.value = value;
 }

 Memory(int id) {
  this.id = id;
 }

 Memory(int id, int value) {
  this.id = id;
  this.value = value;
 }

 public String toString() {
  return "memory:" + id;
 }

}
 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  算法 java string class 作业