我运行完程序发现只剩下一个人了。
代码:
import java.util.Scanner;
import java.io.*;class MergeSortAlgorithms{
final static String MAXVALUE = "zzzzzzzzzzzzz";
static People[] L;
static People[] R;
public static void Merge(People[] A,int p,int q,int r){
int n1=q-p;
int n2=r-q+1;
L = new People[n1+1];
R = new People[n2+1];
for(int i = 0;i < n1;i++)
L[i] = A[p+i];
for(int j = 0;j < n2;j++)
R[j] = A[q+j];
if(L[n1] != null)
L[n1].setName(MAXVALUE);
if(R[n2] != null)
R[n2].setName(MAXVALUE);
int i = 0,j = 0;
for(int k = p; k <= r ;k++){
if(L[i] == null)
continue;
if((L[i] != null)&&(R[j] != null))
if(L[i].CompareTo(R[j])<1){
A[k] = L[i];
i++;
}
else{
A[k] = R[j];
j++;
}
}
}
public static void MergeSort(People[] A,int p,int r){
int q;
if(p<r){
q = (p+r)/2;
MergeSort(A,p,q);
MergeSort(A,q+1,r);
Merge(A,p,q+1,r);
}
}
}class AddressBook{
private People[] entry;
private static int count;
public AddressBook(){
this(25);
}
public AddressBook(int size){
entry = new People[size];
}
public void add(People newPeople){
if(count == entry.length)
enlarge();
entry[count] = newPeople;
count++;
}
public boolean delete(String searchName){
int i;
if (findIndex(searchName) == -1)
return false;
else{
while(findIndex(searchName) != -1){
for(i = findIndex(searchName);i < (count-1);i++)
entry[i] = entry[i+1];
count--;
}
return true;
}
}
public People search(String searchName){
if (findIndex(searchName) == -1)
return null;
else
return entry[findIndex(searchName)];
}
public void sort(){
MergeSortAlgorithms.MergeSort(entry,0,entry.length-1);
}
public void enlarge(){
int i;
People newentry[] = new People [(int)(entry.length * 1.5)];
for(i=0;i<entry.length;i++)
newentry[i] = entry[i];
entry = newentry;
}
public int findIndex(String searchName){
int i;
for (i=0;i<count;i++)
if(entry[i].getName().compareTo(searchName) == 0)
return i;
return -1;
}
public void saveRecord()throws IOException{
int i;
File file = new File("record.txt");
PrintWriter output = new PrintWriter(file);
for(i=0;i<count;i++)
output.print(entry[i].toString());
output.close();
}
public void clearRecord()throws IOException{
int i;
File file = new File("record.txt");
PrintWriter output = new PrintWriter(file);
output.close();
}
public void restoreRecord(){
String temp;
People newPeople = new People();
Scanner input = null;
try{
input = new Scanner(new File("record.txt"));
}
catch (FileNotFoundException e){
e.printStackTrace();
}
while (input.hasNext()){
temp = input.nextLine().trim();
newPeople.setName(temp.substring(temp.indexOf(':')+1,temp.length()));
temp = input.nextLine().trim();
newPeople.setSex(temp.charAt(temp.length()-1));
temp = input.nextLine().trim();
newPeople.setAge(Integer.parseInt((temp.substring(temp.indexOf(':')+1,temp.length())).trim()));
this.add(newPeople);
}
}
}class People{
private String name;
private int age;
private char sex;
private static int count;
public People(){
this("",'M',0);
}
public People(String name, char sex, int age){
setName(name);
setSex(sex);
setAge(age);
count++;
}
public void setName(String name){
this.name = name;
}
public void setSex(char sex){
this.sex = sex;
}
public void setAge(int age){
this.age =age;
}
public String getName(){
return name;
}
public char getSex(){
return sex;
}
public int getAge(){
return age;
}
public int getCount(){
return count;
}
public String toString(){
return "Name:"+getName()+"\nSex:"+getSex()+"\nAge:"+getAge()+"\n";
}
public int CompareTo(People People){
return this.getName().compareTo(People.getName());
}
}public class Test{
public static void main(String ag[])throws IOException{
AddressBook a1 = new AddressBook();
a1.restoreRecord();
a1.sort();
a1.saveRecord();
}
}
测试数据:(record.txt)
Name:Roy
Sex:M
Age:19
Name:Mary
Sex:M
Age:19
Name:Gary
Sex:M
Age:19
Name:Wu
Sex:M
Age:19
Name:Chary
Sex:M
Age:19
Name:Ada
Sex:W
Age:20
Name:Jie
Sex:M
Age:19
Name:Wei
Sex:M
Age:19
Name:Nick
Sex:M
Age:19

解决方案 »

  1.   


      public void restoreRecord(){
            String temp;
            People newPeople = new People();//从头到尾都只有一个People对象newPeople 
                                            //
            Scanner input = null;
            try{
                input = new Scanner(new File("record.txt"));
            }
            catch (FileNotFoundException e){
            e.printStackTrace();
            }
            while (input.hasNext()){
                temp = input.nextLine().trim();
                newPeople.setName(temp.substring(temp.indexOf(':')+1,temp.length()));
                temp = input.nextLine().trim();
                newPeople.setSex(temp.charAt(temp.length()-1));
                temp = input.nextLine().trim();
                newPeople.setAge(Integer.parseInt((temp.substring(temp.indexOf(':')+1,temp.length())).trim()));
                this.add(newPeople);//将一个对象的内容引用该了n次,将同一个引用多次插入
                                        //结果指向的都是最后该了那个对象
                                        //每add都要new个新的,明白了吧
            }
        }
    }
      

  2.   


    //this.add(newPeople);改为
    this.add(new People(newPeople.name,newPeople.sex,age));
      

  3.   

    修改完成,不过MergeSort还是有错