Java索引越界错误

标签 java for-loop indexoutofboundsexception

我的程序中遇到索引越界问题。底部方法中的 for 循环应该按照 FlightSeatingAmount() 方法中指定的次数运行。但我在运行程序进行测试时不断出现错误。有时我在出错之前会得到一两个输出,而有时我不会得到任何输出,只有错误。这是我的代码:

import java.io.FileNotFoundException;
import java.io.PrintWriter;

public class OLE1 {

static String[] airports = {"LAX", "MSP", "FAR", "ATL", "ORD", "DFW", "DEN", "JFK", "SFO", "CLT", "LAS", "PHX", "IAH", "MIA", "PEK", "CAN", "HND", "HKG", "SIN", "LHR", "CDG", "LGW", "MUC", "FRA", "EDI"};
static final int numberOfEntries = 10000;
static String[] firstNames = {"Isis", "Donnette", "Reyes", "Willis", "Kathy", "Elizbeth", "Long", "Jim", "Devorah", "Magda", "Maryetta", "Keturah", "Corrinne", "Shena", "Xiao", "Otha", "Hallie", "Pennie", "Dong", "Kristopher", "Eveline", "Ardella", "Tien", "Tianna", "Loren", "Many", "Anjelica", "Cecile", "Mae", "Jenae", "Sonya", "Dotty", "Florance", "Mittie", "Katia", "Nena", "Lu", "Janee", "Armando", "Leandro", "Claris", "Claudine", "Moriah", "Eddie", "Susan", "Rhoda", "Monnie", "Emelia", "Cory", "Ying"};
static String[] lastNames = {"Belva", "Nam", "Liz", "Jeanett", "Corine", "Abe", "Olga", "Olevia", "Ernestine", "Joanne", "Sharyn", "Heidi", "Zachariah", "Sylvester", "Luetta", "Stephaine", "Garrett", "Debby", "Judi", "Noe", "Maybelle", "Eldora", "Roseann", "Madge", "Glayds", "Eleonore", "Josephine", "Quincy", "Alyson", "Earlene", "Clementina", "Jeri", "Kristel", "Carrol", "Zona", "Eileen", "Margherita", "Joline", "Terence", "Christinia", "Eldon", "Arleen", "Aimee", "Chanda", "Carin", "Prudence", "Tanja", "Kathlene", "Kareen", "Geneva"};
static String[] middleInitial = {"A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W", "X", "Y", "Z"};
static int day = (int)(Math.random() * 21);

public static void main(String args[]) throws FileNotFoundException {

    System.out.println("Welcome to the airplane program. This program will go ahead and create a list of all the passengers that have gone through 25 different airports.");
    System.out.println("The list of the airports we keep track of are listed below\n");

    //for listing out the airport names
    int count = 0;
    for (int i = 0; i < 5; i++) {
        System.out.print(airports[count]);
        count++;
        System.out.print(" | " + airports[count]);
        count++;
        System.out.print(" | " + airports[count]);
        count++;
        System.out.print(" | " + airports[count]);
        count++;
        System.out.println(" | " + airports[count]);
        count++;
    }

    flightManifest();

}

public static int flightSeatingAmount() {
    int flight = (int)(50 + (Math.random() * 800));
    return flight;
}

public static String nameGenerator() {
    String names = "";
    int firstAndLast = (int)(Math.random() * 50);
    int initial = (int)(Math.random() * 26);
    names = lastNames[firstAndLast] + "," + firstNames[firstAndLast] + "," + middleInitial[initial];
    return names;
}

public static int flightTimeIntervel() {
    int timing[] = {15, 30, 45, 00};
    int randomTiming = (int)(Math.random() * 4);
    return timing[randomTiming];
}

public static int airportSelectionOne() {
    int airportOne = (int)(Math.random() * 49);
    return airportOne;
}

public static int airportSelectionTwo() {
    int airportTwo = (int)(Math.random() * 49);
    return airportTwo;
}

public static void flightManifest() throws FileNotFoundException {

    System.out.println(flightSeatingAmount());
    for (int i = 0; i <= flightSeatingAmount(); i++) {
        System.out.println(nameGenerator() + "," + "0" + "," + airports[airportSelectionOne()]);
    }

}

}

这是我得到的输出:

Welcome to the airplane program. This program will go ahead and create a list of all         the passengers that have gone through 25 different airports.
The list of the airports we keep track of are listed below

LAX | MSP | FAR | ATL | ORD
DFW | DEN | JFK | SFO | CLT
LAS | PHX | IAH | MIA | PEK
CAN | HND | HKG | SIN | LHR
CDG | LGW | MUC | FRA | EDI
843
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 45
    at OLE1.flightManifest(OLE1.java:70)
    at OLE1.main(OLE1.java:33)

843 指的是循环应该执行的次数。

最佳答案

您的 airportSelectionOne 返回 45。您的机场数组中没有 46 个机场。这意味着您已经超出了数组的末尾,这是非法的。更改您的函数,使其返回的数字不会大于数组的大小。提示:您可以通过 airports.length 得知数组的大小

关于Java索引越界错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21593098/

相关文章:

java - 手机待机屏幕关闭时识别语音?

arrays - Cypress 中的迭代数组

python - 我在范围内时收到 IndexError

java - 我该怎么做才能让我的程序不出现 IndexOutOfBounds 错误?

java - LDAP 与 CAS + Spring 集成

java - 将 PST 格式的日期和时间转换为 UTC 格式

java - mac 上的 logback 返回问号而不是单词

java - 在 for 循环声明中初始化的变量的范围实际上不仅仅是 block 范围吗?

java - for 循环(i) 在循环外使用变量或可能的解决方案?

javascript - Javascript 中数组索引不会越界