?? comparatortest.java
字號:
import javax.microedition.midlet.*;
import javax.microedition.lcdui.*;
import javax.microedition.rms.*;
import java.io.*;
public class ComparatorTest extends MIDlet implements CommandListener{
Display display;
RecordStore generalStore;
Command exitCmd,menuCmd;
Command sortByMCmd;
Command sortByPCmd;
TextBox results;
String[] names = {"呂布", "趙云", "諸葛亮"};
int[] military = {100, 98, 70};
int[] politics = {15, 80, 100};
int numOfRecords;
public ComparatorTest(){
display = Display.getDisplay(this);
results = new TextBox("武力排序", "", 10000, TextField.ANY);
exitCmd = new Command("退出",Command.EXIT,1);
menuCmd=new Command("Menu",Command.OK,1);
sortByMCmd = new Command("以武力大小排序",Command.SCREEN,1);
sortByPCmd = new Command("以智力大小排序",Command.SCREEN,2);
results.addCommand(exitCmd);
results.addCommand(menuCmd);
results.addCommand(sortByMCmd);
results.addCommand(sortByPCmd);
results.setCommandListener(this);
try{
generalStore = RecordStore.openRecordStore("general", true);
numOfRecords = generalStore.getNumRecords();
}
catch(Exception ex){}
if(numOfRecords == 0){
for(int i=0;i<names.length;i++){
General general = new General(names[i], military[i], politics[i]);
general.writeToRecord(generalStore);
}
}
}
public void startApp(){
sort(1);
display.setCurrent(results);
}
public void sort(int choice){
String sortingResult = "";
MyComparator rc = new MyComparator();
rc.setComparedItem(choice);
try{
RecordEnumeration renum = generalStore.enumerateRecords(null, rc, false);
while(renum.hasNextElement()){
General general = new General();
general.readFromRecord(generalStore, renum.nextRecordId());
sortingResult += ""+general.name+" ";
sortingResult += (choice == 1)?""+general.military+"\n"
:""+general.politics+"\n";
}
renum.destroy();
}
catch(Exception ex){}
results.setTitle((choice == 1)?"武力排序":"智力排序");
results.setString(sortingResult);
}
public void pauseApp(){
}
public void destroyApp(boolean unconditional){
try{
generalStore.closeRecordStore();
}
catch(Exception ex){}
}
public void commandAction(Command c, Displayable d){
if(c == exitCmd){
destroyApp(true);
notifyDestroyed();
}
else if(c == sortByMCmd){
sort(1);
display.setCurrent(results);
}
else if(c == sortByPCmd){
sort(2);
display.setCurrent(results);
}
}
}
class General{
String name;
int military;
int politics;
public General(){
}
public General(String name, int military, int politics){
this.name = name;
this.military = military;
this.politics = politics;
}
public void writeToRecord(RecordStore rs){
byte[] data = null;
try{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
DataOutputStream dos = new DataOutputStream(baos);
dos.writeUTF(name);
dos.writeInt(military);
dos.writeInt(politics);
data = baos.toByteArray();
rs.addRecord(data, 0, data.length);
baos.close();
dos.close();
}
catch(Exception ex){
}
}
public void readFromRecord(RecordStore rs, int RecordID){
try{
byte[] data = rs.getRecord(RecordID);
ByteArrayInputStream bais = new ByteArrayInputStream(data);
DataInputStream dis = new DataInputStream(bais);
name = new String(dis.readUTF());
military = dis.readInt();
politics = dis.readInt();
bais.close();
dis.close();
}
catch(Exception ex){}
}
}
class MyComparator implements RecordComparator{
int comparedItem = 1;
public void setComparedItem(int item){
if(item == 1) comparedItem = 1;
else comparedItem = 2;
}
public int compare(byte[] rect1, byte[] rect2){
int military1 = 0, military2 = 0;
int politics1 = 0, politics2 = 0;
try{
ByteArrayInputStream bais1 = new ByteArrayInputStream(rect1);
DataInputStream dis1 = new DataInputStream(bais1);
ByteArrayInputStream bais2 = new ByteArrayInputStream(rect2);
DataInputStream dis2 = new DataInputStream(bais2);
String name1 = dis1.readUTF();
military1 = dis1.readInt();
politics1 = dis1.readInt();
String name2 = dis2.readUTF();
military2 = dis2.readInt();
politics2 = dis2.readInt();
bais1.close();
dis1.close();
bais2.close();
dis2.close();
}
catch(Exception ex){}
if(comparedItem == 1){
if(military1 > military2) return RecordComparator.PRECEDES;
else if(military1 < military2) return RecordComparator.FOLLOWS;
else return RecordComparator.EQUIVALENT;
}
else{
if(politics1 > politics2) return RecordComparator.PRECEDES;
else if(politics1 < politics2) return RecordComparator.FOLLOWS;
else return RecordComparator.EQUIVALENT;
}
}
}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -