2D ArrayOutOfBounds异常

时间:2016-10-09 00:18:11

标签: arrays 2d indexoutofboundsexception

我正在编写一个允许用户输入状态的程序,它将回显输入的状态,以及状态鸟和花。我得到了一个ArrayOutOfBounds异常,我没有&#39 ;我知道这是怎么发生的。这发生在printArray方法

public class stateInformation
{
public static void searchState(String state)
{
    int row, col;
    String[][] states =
        {
            {"Alabama","Camellia","Yellowhammer"},
            {"Alaska","Forget-Me-Not","Willow Ptarmigan"},
            {"Arizona","Saguaro Cactus","Cactus Wren"},
            {"Arkansas","Apple blossom","Mockingbird"},
            {"California","Golden Poppy","California Valley Quail"},
            {"Colorado","Rocky Mountain Columbine","Lark Bunting"},
            {"Connecticut","Mountain Laurel","Robin"},
            {"Delaware","Peach Blossom","Blue Hen Chicken"},
            {"Florida","Orange Blossom","Mockingbird"},
            {"Georgia","Cherokee Rose","Brown Thrasher"},
            {"Hawaii","Hibiscus","Nene"},
            {"Idaho","Syringa","Mountain Bluebird"},
            {"Illinois","Native Violet","Cardinal"},
            {"Indiana","Peony","Cardinal"},
            {"Iowa","Wild Rose","Eastern Goldfinch"},
            {"Kansas","Native Sunflower","Western Meadowlark"},
            {"Kentucky","Goldenrod","Cardinal"},
            {"Louisiana","Magnolia","Eastern Brown Pelican"},
            {"Maine","Pine Cone & Tassle","Chickadee"},
            {"Maryland","Black Eyed Susan","Baltimore Oriole"},
            {"Massachusettes","Mayflower","Chickadee"},
            {"Michigan","Apple Blossom","Robin"},
            {"Minnesota","Lady Slipper","Common Loon"},
            {"Mississippi","Magnolia","Mockingbird"},
            {"Missouri","Hawthorn","Bluebird"},
            {"Montana","Bitterroot","Western Meadowlark"},
            {"Nebraska","Goldenrod","Western Meadowlark"},
            {"Nevada","Sagebrush","Mountain Bluebird"},
            {"New Hampshire","Purple Lilac","Purple Finch"},
            {"New Jersey","Purple Violet","Eastern Goldfinch"},
            {"New Mexico","Yucca","Roadrunner"},
            {"New York","Rose","Bluebird"},
            {"North Carolina","Dogwood","Cardinal"},
            {"North Dakota","Wild Prairie Rose","Western Meadowlark"},
            {"Ohio","Scarlet Carnation","Cardinal"},
            {"Oklahoma","Mistletoe","Scissor-Tailed Flycatcher"},
            {"Oregon","Oregon Grape","Western Meadowlark"},
            {"Pennsylvania","Mountain Laurel","Ruffed Grouse"},
            {"Rhode Island","Violet","Rhode Island Red"},
            {"South Carolina","Yellow Jessamine","Great Carolina Wren"},
            {"South Dakota","Pasque Flower","Ring-Necked Pheasant"},
            {"Tennessee","Purple Iris","Mockingbird"},
            {"Texas","Texas Blue Bonnet","Mockingbird"},
            {"Utah","Sego Lily","American Seagull"},
            {"Vermont","Red Clover","Hermit Thrush"},
            {"Virginia","Dogwood","Cardinal"},
            {"Washington","Western Rhododendron","Willow Goldfinch"},
            {"West Virginia","Rhododendron","Cardinal"},
            {"Wisconsin","Wood Violet","Robin"},
            {"Wyoming","Indian Paint Brush","Western Meadowlark"},
        };


for(row=0;row<states.length;row++)
{
    for(col=0;col<states[row].length;col++)
    {
        if(states[row][col].equals(state))
        {
            printArray(states,row);
        }
    }
}




}
public static void printArray(String[][] array, int row)
{
    for(int i=0;row<array[row].length;i++)
    {
        System.out.println(array[row][i]);
    }
}

public static void main(String[] args)
{
    searchState("Arizona");
}
}           

1 个答案:

答案 0 :(得分:1)

检查主方法中的循环行总是&lt; array [row] .length并且由于无限循环,你到达了一个不存在的数组索引,你正在访问它,所以它给出了这个错误。 希望答案清楚