Java程序数组越界问题,请各路高手指点 15
packagemrh;importjava.io.File;importmrh.util.FileReaderUtil;importmrh.util.RandomUtil...
package mrh;
import java.io.File;
import mrh.util.FileReaderUtil;
import mrh.util.RandomUtil;
public class Grouping {
public static void main(String[] args) {
Grouping h = new Grouping();
// 1. 从指定文件中读取包含学生姓名及学号的数组
String[] students = h.getStudents(new File("list.txt"));
// 2. 对学生数组students进行随机排序
students = h.group(students);
// 3. 打印出分组信息
h.printGroupingInfo(students, 3);
}
// 从指定文件中读取包含学生姓名及学号的数组
public String[] getStudents(File listFile) {
FileReaderUtil fru = new FileReaderUtil(listFile);
String[] students = fru.readLines();
return students;
}
// 对学生数组students进行排序, 随机数可以利用RandomUtil的generatInt(int max)产生
public String[] group(String[] students) {
int g = RandomUtil.generatInt(students.length - 1);
int m=0;
for (;m<=g;m++){
int i = 0;
int j = 0;
String[] stu1 = new String[students.length];
while (i<=28){
j = 2*i;
stu1[j] = students[i];
i++;
}
int k = (students.length-1)/2;
for (; i < students.length; i++) {
for (;k>=0;k++){
j=i-k;
stu1[j] = students[i];
}
}
while (j<=28){
i=2*j+1;
students[i]=stu1[j];
j++;
}
int n = (students.length+1)/2;
for (; j < stu1.length; j++) {
for (;n>=0;n--){
i=j-n;
students[i] = stu1[j];
}
}
}
return students;
}
// 打印出分组信息
public void printGroupingInfo(String[] students, int countMember) {
int u=1;
int i=0;
while ( u<=students.length/3){
System.out.println("第"+u+"组:");
for (; i < 3*u; i++) {
System.out.println(students[i]);
}
u++;
}
}
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at mrh.Grouping.group(Grouping.java:45)
at mrh.Grouping.main(Grouping.java:13)
就是 students = h.group(students);
stu1[j] = students[i];
students = h.group(students);这三个地方显示有问题 展开
import java.io.File;
import mrh.util.FileReaderUtil;
import mrh.util.RandomUtil;
public class Grouping {
public static void main(String[] args) {
Grouping h = new Grouping();
// 1. 从指定文件中读取包含学生姓名及学号的数组
String[] students = h.getStudents(new File("list.txt"));
// 2. 对学生数组students进行随机排序
students = h.group(students);
// 3. 打印出分组信息
h.printGroupingInfo(students, 3);
}
// 从指定文件中读取包含学生姓名及学号的数组
public String[] getStudents(File listFile) {
FileReaderUtil fru = new FileReaderUtil(listFile);
String[] students = fru.readLines();
return students;
}
// 对学生数组students进行排序, 随机数可以利用RandomUtil的generatInt(int max)产生
public String[] group(String[] students) {
int g = RandomUtil.generatInt(students.length - 1);
int m=0;
for (;m<=g;m++){
int i = 0;
int j = 0;
String[] stu1 = new String[students.length];
while (i<=28){
j = 2*i;
stu1[j] = students[i];
i++;
}
int k = (students.length-1)/2;
for (; i < students.length; i++) {
for (;k>=0;k++){
j=i-k;
stu1[j] = students[i];
}
}
while (j<=28){
i=2*j+1;
students[i]=stu1[j];
j++;
}
int n = (students.length+1)/2;
for (; j < stu1.length; j++) {
for (;n>=0;n--){
i=j-n;
students[i] = stu1[j];
}
}
}
return students;
}
// 打印出分组信息
public void printGroupingInfo(String[] students, int countMember) {
int u=1;
int i=0;
while ( u<=students.length/3){
System.out.println("第"+u+"组:");
for (; i < 3*u; i++) {
System.out.println(students[i]);
}
u++;
}
}
}
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1
at mrh.Grouping.group(Grouping.java:45)
at mrh.Grouping.main(Grouping.java:13)
就是 students = h.group(students);
stu1[j] = students[i];
students = h.group(students);这三个地方显示有问题 展开
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询