调试错误为:
Exception in thread "main" java.lang.StackOverflowError
at java.util.ArrayList.get(ArrayList.java:322)
at absenceinfo.absenceInfo.swap(absenceInfo.java:205)
at absenceinfo.absenceInfo.partition(absenceInfo.java:218)
at absenceinfo.absenceInfo.recQuickSort(absenceInfo.java:269)
我认我是快速排序算法中出现了递归死循环,不过整么看也看不出来,请大家帮我开开是哪里出错了。
相关的代码:
public class absenceInfo
{
/*包含一个范型数组列表,用于存储学生请假信息*/
private ArrayList<studentinfo> info = new ArrayList<studentinfo>();
/*存储搜索算法找到的符合要求的类对象在数组中的下标值*/
private ArrayList<Integer> indexArray = new ArrayList<Integer>();
private int count;/*记录读入的总studentinfo数(当前请假记录数)*/
private int resultNum;//记录indexArray中存储的类对象个数
private int searchWay;//标记查找方式 /*构造函数*/
public absenceInfo()
{
count = info.size();//用当前数组列表中所含记录数来初始化count
resultNum = 0;
searchWay = 1;//设置默认查找方式
} /*根据外部选择设置查找方式*/
public void setSearchWay(int way)
{
searchWay = way;
} /*从指定文件中读入数据*/
public void readin(String str) throws IOException
{
FileReader fr = null;
try {
fr = new FileReader(str);
}
catch (FileNotFoundException ex)
{
Logger.getLogger(absenceInfo.class.getName()).log(Level.SEVERE, null, ex);
}
BufferedReader br = new BufferedReader(fr);//将FR包装为buffereReader
String temp = br.readLine();//读取一行数据存到字符串temp中
//当temp字符串中读取到数据时将其以实体类studentinfo的形式写入到数组列表中
while (temp != null)
{
//从temp中截取各个数据段
studentinfo tempStudentInfo = new studentinfo();
tempStudentInfo.snum =temp. substring(0, 9).trim();
tempStudentInfo.sname =temp. substring(9, 13).trim();
tempStudentInfo.sdate =temp. substring(13, 21).trim();
tempStudentInfo.edate =temp. substring(21, 29).trim();
tempStudentInfo.reason =temp. substring(29, 31).trim();
//将读取到的studentinfo类的实体类数据存到ArrayList<studentinfo> info中去
info.add(tempStudentInfo);
//读取下一行
temp = br.readLine();
}
count = info.size();/*将count的值更改为ArrayList<studentinfo> info中类对象的个数*/
br.close();
fr.close();
}
} private void swap(int first,int second) throws StackOverflowError
{ studentinfo tempNode = new studentinfo(); tempNode = info.get(first);
info.set(first,info.get(second));
info.set(second,tempNode); } private int partition(int first,int last)
{
studentinfo pivot = new studentinfo(); int index, smallIndex; swap(first, (int)((first+last)/2)); pivot =info.get(first);
smallIndex = first; for(index = first + 1;index <= last;index++)
{
switch(searchWay)
{
case 1://按学号排序
{
if((info.get(index).snum).compareTo(pivot.snum)<0)
{
smallIndex++;
swap(smallIndex, index);
}
break;
}
case 2://按姓名排序
{
if((info.get(index).sname).compareTo(pivot.sname)<0)
{
smallIndex++;
swap(smallIndex, index);
}
break;
}
case 3://按原因排序
{
if((info.get(index).sname).compareTo(pivot.sname)<0)
{
smallIndex++;
swap(smallIndex, index);
}
break;
}
default:
System.out.println("查询方式选择错误");
break;
}
}
swap(first, smallIndex);
return 0;
} private void recQuickSort(int first, int last)
{
int pivotLocation;
if(first < last)
{
pivotLocation = partition(first,last);
recQuickSort(first,pivotLocation - 1);
recQuickSort(pivotLocation + 1,last);
}
} public void quickSort()
{
recQuickSort(0,this.count-1);
} public static void main(String[] args) throws IOException
{
absenceInfo a = new absenceInfo();
a.readin("absenceInfo.txt");
a.setSearchWay(1);//根据界面中选项设置查找类型
a.quickSort();
a.binarySearch("081110201");
}
}class studentinfo//存储学生请假信息实体类
{
public studentinfo()
{
snum = "";
sname = "";
sdate = "";
edate = "";
reason = "";
} public String snum;//学号
public String sname;//姓名
public String sdate;//请假开始日期
public String edate;//请假结束时期
public String reason;//请假原因
}
Exception in thread "main" java.lang.StackOverflowError
at java.util.ArrayList.get(ArrayList.java:322)
at absenceinfo.absenceInfo.swap(absenceInfo.java:205)
at absenceinfo.absenceInfo.partition(absenceInfo.java:218)
at absenceinfo.absenceInfo.recQuickSort(absenceInfo.java:269)
我认我是快速排序算法中出现了递归死循环,不过整么看也看不出来,请大家帮我开开是哪里出错了。
相关的代码:
public class absenceInfo
{
/*包含一个范型数组列表,用于存储学生请假信息*/
private ArrayList<studentinfo> info = new ArrayList<studentinfo>();
/*存储搜索算法找到的符合要求的类对象在数组中的下标值*/
private ArrayList<Integer> indexArray = new ArrayList<Integer>();
private int count;/*记录读入的总studentinfo数(当前请假记录数)*/
private int resultNum;//记录indexArray中存储的类对象个数
private int searchWay;//标记查找方式 /*构造函数*/
public absenceInfo()
{
count = info.size();//用当前数组列表中所含记录数来初始化count
resultNum = 0;
searchWay = 1;//设置默认查找方式
} /*根据外部选择设置查找方式*/
public void setSearchWay(int way)
{
searchWay = way;
} /*从指定文件中读入数据*/
public void readin(String str) throws IOException
{
FileReader fr = null;
try {
fr = new FileReader(str);
}
catch (FileNotFoundException ex)
{
Logger.getLogger(absenceInfo.class.getName()).log(Level.SEVERE, null, ex);
}
BufferedReader br = new BufferedReader(fr);//将FR包装为buffereReader
String temp = br.readLine();//读取一行数据存到字符串temp中
//当temp字符串中读取到数据时将其以实体类studentinfo的形式写入到数组列表中
while (temp != null)
{
//从temp中截取各个数据段
studentinfo tempStudentInfo = new studentinfo();
tempStudentInfo.snum =temp. substring(0, 9).trim();
tempStudentInfo.sname =temp. substring(9, 13).trim();
tempStudentInfo.sdate =temp. substring(13, 21).trim();
tempStudentInfo.edate =temp. substring(21, 29).trim();
tempStudentInfo.reason =temp. substring(29, 31).trim();
//将读取到的studentinfo类的实体类数据存到ArrayList<studentinfo> info中去
info.add(tempStudentInfo);
//读取下一行
temp = br.readLine();
}
count = info.size();/*将count的值更改为ArrayList<studentinfo> info中类对象的个数*/
br.close();
fr.close();
}
} private void swap(int first,int second) throws StackOverflowError
{ studentinfo tempNode = new studentinfo(); tempNode = info.get(first);
info.set(first,info.get(second));
info.set(second,tempNode); } private int partition(int first,int last)
{
studentinfo pivot = new studentinfo(); int index, smallIndex; swap(first, (int)((first+last)/2)); pivot =info.get(first);
smallIndex = first; for(index = first + 1;index <= last;index++)
{
switch(searchWay)
{
case 1://按学号排序
{
if((info.get(index).snum).compareTo(pivot.snum)<0)
{
smallIndex++;
swap(smallIndex, index);
}
break;
}
case 2://按姓名排序
{
if((info.get(index).sname).compareTo(pivot.sname)<0)
{
smallIndex++;
swap(smallIndex, index);
}
break;
}
case 3://按原因排序
{
if((info.get(index).sname).compareTo(pivot.sname)<0)
{
smallIndex++;
swap(smallIndex, index);
}
break;
}
default:
System.out.println("查询方式选择错误");
break;
}
}
swap(first, smallIndex);
return 0;
} private void recQuickSort(int first, int last)
{
int pivotLocation;
if(first < last)
{
pivotLocation = partition(first,last);
recQuickSort(first,pivotLocation - 1);
recQuickSort(pivotLocation + 1,last);
}
} public void quickSort()
{
recQuickSort(0,this.count-1);
} public static void main(String[] args) throws IOException
{
absenceInfo a = new absenceInfo();
a.readin("absenceInfo.txt");
a.setSearchWay(1);//根据界面中选项设置查找类型
a.quickSort();
a.binarySearch("081110201");
}
}class studentinfo//存储学生请假信息实体类
{
public studentinfo()
{
snum = "";
sname = "";
sdate = "";
edate = "";
reason = "";
} public String snum;//学号
public String sname;//姓名
public String sdate;//请假开始日期
public String edate;//请假结束时期
public String reason;//请假原因
}
如果数字下标的左边 -右边〉0 意味着虚幻已经结束temp != null 永远不会知道循环是否结束