下面是未采用变址存取的实现 //存货类 public class Stock { int stockID; // 编号,编号必须是0-9999 String category; // 类别 String goodName; // 商品名 int amount; // 数量 private static Stock[] stocks = new Stock[10000]; // 不采取变址存取方法,则只有一张主查询表即stocks static { for(int i=0;i<stocks.length;i++) { stocks[i] = new Stock(); } } public Stock() { //空构造函数 } public Stock(int stockID, String category, String goodName, int amount) { // 这里为简单起见就不检查编号、类别、商品名还有数量是否合法 // 了,并且假设每次新加入的Stock的stockID都不与前面的重复 this.stockID = stockID; this.category = category; this.goodName = goodName; this.amount = amount; stocks[stockID] = this; // 直接通过stockID存取相应的存货到stocks中,这样之后就可以直接通过stockID查询具体存货信息 } public int getStockID() { return stockID; } public void setStockID(int stockID) { this.stockID = stockID; } public String getCategory() { return category; } public void setCategory(String category) { this.category = category; } public String getGoodName() { return goodName; } public void setGoodName(String goodName) { this.goodName = goodName; } public int getAmount() { return amount; } public void setAmount(int amount) { this.amount = amount; } public static Stock[] getStocks() { return stocks; } public static void main(String[] args) { Stock[] stocks = Stock.getStocks(); // 获取所有存货,当前没有任何存货 System.out.println(stocks[0]); // 所以打印出来的是null // 往stocks中加入存货 Stock stock1 = new Stock(0, "fruit", "apple", 100); // 水果分类,苹果100箱 Stock stock2 = new Stock(235, "vegetable", "carrot", 200); // 蔬菜分类,胡萝卜200箱 stocks = Stock.getStocks(); // 获取现有的所有存货信息 System.out.println(); // 下面直接从主查询表中根据stockID获取相应的存货信息,并打印 Stock stock3 = stocks[1]; Stock stock4 = stocks[235]; System.out.println("stock3存货信息如下:"); if(stock3==null) { System.out.println("没有对应stock3的存货!"); } else { System.out.println("存货类别:" + stock3.getCategory()); System.out.println("存货品名: " + stock3.getGoodName()); System.out.println("存货数量: " + stock3.getAmount()); } System.out.println(); System.out.println("stock4存货信息如下:"); if(stock4==null) { System.out.println("没有对应stock4的存货!"); } else { System.out.println("存货类别:" + stock4.getCategory()); System.out.println("存货品名: " + stock4.getGoodName()); System.out.println("存货数量: " + stock4.getAmount()); } } } 阶梯存取方法学生成绩的实现: /* * ≥ 90 % A * < 90.0% B * < 77.5% C 即65分到77.5分之间为C * < 65.0% D 即50分到65分之间为D * < 50.0% E 即50分以下为E */ public class StudentScore { private static final int maxGradeLevel = 4; private static double[] rangeLimits = new double[5]; //分数界限 private static char[] grades = new char[5]; //等级,包括A,B,C,D,E,F static { rangeLimits[0] = 50.0; grades[0] = 'E'; rangeLimits[1] = 65.0; grades[1] = 'D'; rangeLimits[2] = 77.5; grades[2] = 'C'; rangeLimits[3] = 90.0; grades[3] = 'B'; rangeLimits[4] = 100.0; grades[4] = 'A'; } //根据学生分数获得相应级别描述字符:[ABCDEF] public static char getStudentGrade(double score) { int level = 0; char grade = 'A'; while(grade=='A' && level<maxGradeLevel) { if(score<rangeLimits[level]) { grade = grades[level]; System.out.println("当前级别是:" + grade +"/t Level是:" + level); break; } else { level++; } } return grade; } public static void main(String[]args) { System.out.println("78分学生的级别是:" + StudentScore.getStudentGrade(78)); } }