Java数组索引越界异常修复

Java数组索引越界异常修复,java,indexoutofboundsexception,Java,Indexoutofboundsexception,我当前在执行行name.firstName=this.firstNames[rand.nextInt(NUM_NAMES)]时遇到数组越界异常通常情况下,我在查找这些异常的源代码时不会遇到问题,但是我已经在这个问题上纠缠了一段时间了。感谢您的帮助,class和stacktrace粘贴在下面: public class NameGenerator { private static final int NUM_NAMES = 200; private static final Fil

我当前在执行行
name.firstName=this.firstNames[rand.nextInt(NUM_NAMES)]时遇到数组越界异常通常情况下,我在查找这些异常的源代码时不会遇到问题,但是我已经在这个问题上纠缠了一段时间了。感谢您的帮助,class和stacktrace粘贴在下面:

public class NameGenerator {
    private static final int NUM_NAMES = 200;
    private static final File NAMES_FILE = new File("resources/info.dat");

    /** a random number generator */
    private Random rand;

    /** the array of first names */
    private String[] firstNames;
    /** the array of last names */
    private String[] lastNames;

    /**
     * Default Constructor
     */
    public NameGen() {
        super();
        this.rand = new Random();

        try {
            readFile();
        } catch (IOException exp) {
            this.first = new String[] { "foo" };
            this.last = new String[] { "bar" };
        }
    }

    /**
     * Read the names from the file
     */
    private void readNFiles() throws IOException {
        List<String> tempFirst = new ArrayList<String>();
        List<String> tempLast = new ArrayList<String>();

        Scanner scnr = new Scanner(NAMES_FILE);

        while (scnr.hasNext()) {
            tempFirst.add(scnr.next());
            tempLast.add(scnr.next());
        }

        scnr.close();

        int size = tempFirst.size();

        this.first = new String[size];
        tempFirst.toArray(this.firstNames);

        this.last = new String[size];
        tempLast.toArray(this.last);
    }

    /**
     * @return a generated name
     */
    public FullName generateName() {
        FullName name = new FullName();
        name.first = this.firstNames[rand.nextInt()];

        name.last = this.lastNames[rand.nextInt()];
        return name;
    }

    /**
     * Class describing a full name
     */
    public static final class FullName {
        /** the first name */
        public String firstName;
        /** the last name */
        public String lastName;
    }
}
公共类名称生成器{
私有静态final int NUM_NAMES=200;
私有静态最终文件名_File=新文件(“resources/info.dat”);
/**随机数发生器*/
私有随机兰德;
/**名字数组*/
私有字符串[]名;
/**姓氏数组*/
私有字符串[]lastNames;
/**
*默认构造函数
*/
public NameGen(){
超级();
this.rand=new Random();
试一试{
readFile();
}捕获(IOEXP异常){
this.first=新字符串[]{“foo”};
this.last=新字符串[]{“bar”};
}
}
/**
*从文件中读取名称
*/
私有void readNFiles()引发IOException{
List tempFirst=new ArrayList();
List tempLast=new ArrayList();
扫描仪scnr=新扫描仪(名称\u文件);
while(scnr.hasNext()){
tempFirst.add(scnr.next());
add(scnr.next());
}
scnr.close();
int size=tempFirst.size();
this.first=新字符串[大小];
tempFirst.toArray(这个名字);
this.last=新字符串[大小];
tempLast.toArray(这个是最后一个);
}
/**
*@返回生成的名称
*/
公共全名generateName(){
全名=新全名();
name.first=this.firstNames[rand.nextInt()];
name.last=this.lastNames[rand.nextInt()];
返回名称;
}
/**
*类描述全名
*/
公共静态最终类全名{
/**名字*/
公共字符串名;
/**姓*/
公共字符串lastName;
}
}
基于

try {

    readNamesFiles();

} catch (IOException exp) {

    this.firstNames = new String[] { "John" };
    this.lastNames = new String[] { "Doe" };

}
不能保证数组将包含
NUM\u name
元素(至少应该记录异常)

所以使用类似于
name.firstName=this.firstName[rand.nextInt(NUM_NAMES)]的东西有可能导致一些严重问题

相反,你应该用现实而不是假设,使用更像

name.firstName = this.firstNames[rand.nextInt(this.firstNames.length)];

以下是问题代码的摘要:

List<String> tempFirstNames = new ArrayList<String>(NUM_NAMES);
int size = tempFirstNames.size();

this.firstNames = new String[size];
FullName name = new FullName();
name.firstName = this.firstNames[rand.nextInt(NUM_NAMES)];

可能
rand.nextInt(NUM\u NAMES-1)
?未修复此问题:(我看不出你所谓的<代码>世代> <代码>。考虑提供一个演示你的问题的代码。这不是一个代码转储,而是你正在做的一个例子,它突出了你所面临的问题。这将导致更少的混乱和更好的响应。你也忽略了<代码> IOExux<代码>。It’这远低于您的预期。我认为您也应该根据实际情况而不是假设工作,尝试使用
rand.nextInt(firstNames.length)
(和
rand.nextInt(lastNames.length)
name.firstName = this.firstNames[rand.nextInt(firstNames.length)];