读书人

数组越界有关问题

发布时间: 2012-06-14 16:00:31 作者: rapoo

数组越界问题
程序一点运行就出错误,错误如下:
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 0
at hignWay.Day0_HelloWorld.main(Day0_HelloWorld.java:54)

完整代码如下:
package hignWay;

import java.util.Vector;
//主
public class Day0_HelloWorld
{
private static StringBuffer goToTheEnd = new StringBuffer();//存路(字符串,用于打印出)
private static Station stationArr[] = new Station[14];//存路(站的象,size自行整)
private static int index = 0;//stationArr[]的下
private static intcount = 0;//路
private static Vector<StringBuffer> SBVector = new Vector<StringBuffer>();//用来存各各的路径
private static Vector<Station> StationVector = new Vector<Station>();
//main函
public static void main(String[] args)
{
/*-------------------各站行初期化-------------------*/
/*可以自行路行展,展了以後忘了初期化,加一站,周的都受影*/
/* 世界地 */
/*天堂--沼--山丘 彩虹*/
/* | | |*/
/*草原--冥河----地--*/
/* | |*/
/* 荒漠--洲--古墓--洞穴*/
Station tiantang = new Station("天堂");StationVector.add(tiantang);
Station zhaoze = new Station("沼");StationVector.add(zhaoze);
Station shanqiu = new Station("山丘");StationVector.add(shanqiu);
Station caoyuan = new Station("草原");StationVector.add(caoyuan);
Station minghe = new Station("冥河");StationVector.add(minghe);
Station emeng = new Station("");StationVector.add(emeng);
Station diyu = new Station("地");StationVector.add(diyu);
Station lianyu = new Station("");StationVector.add(lianyu);
Station caihong = new Station("彩虹");StationVector.add(caihong);
Station huangmo = new Station("荒漠");StationVector.add(huangmo);
Station lvzhou = new Station("洲");StationVector.add(lvzhou);
Station gumu = new Station("古墓");StationVector.add(gumu);
Station dongxue = new Station("洞穴");StationVector.add(dongxue);

tiantang.setNextStation(zhaoze, caoyuan);
zhaoze.setNextStation(tiantang, shanqiu, minghe);
shanqiu.setNextStation(zhaoze);
caoyuan.setNextStation(tiantang, minghe, huangmo);
minghe.setNextStation(zhaoze, caoyuan, emeng);
emeng.setNextStation(minghe, diyu, gumu);
diyu.setNextStation(emeng, lianyu, caihong);
lianyu.setNextStation(diyu);
caihong.setNextStation(diyu);
huangmo.setNextStation(caoyuan, lvzhou);
lvzhou.setNextStation(huangmo, gumu);
gumu.setNextStation(lvzhou, emeng, dongxue);
dongxue.setNextStation(gumu);
/*-------------------各站行初期化-------------------*/

//找路始Start to End
Station start = getStation(args[0],StationVector);
Station end = getStation(args[1],StationVector);
if(start.stationName.equals("的站") || end.stationName.equals("的站")) return;
Day0_HelloWorld.funcStartToEnd(start,end);

System.out.println("共有"+count+"走法,下面列出最短的"+(count<=3?count:3)+"");

//路Vector按升序排序并出前几路
int tempI = 0,tempJ;
StringBuffer tempSB = new StringBuffer();
for(;tempI<SBVector.size()-1;tempI++)
{
for(tempJ=tempI+1;tempJ<SBVector.size();tempJ++)
{
if(SBVector.get(tempI).length()>SBVector.get(tempJ).length())
{
tempSB = SBVector.get(tempI);
SBVector.setElementAt(SBVector.get(tempJ), tempI);
SBVector.setElementAt(tempSB, tempJ);
}
}
}
for(tempI=0;tempI<(count<=3?count:3)/*SBVector.size()*/;tempI++)
System.out.println("Road_"+tempI+": "+SBVector.get(tempI));
}

public static void funcStartToEnd(Station start, Station end)
{
stationArr[index++] = start;//保存起始站


int i = 0;

if(!start.equals(end))//起始站和束站不是同一站
{
//向下一站出,逐遍周能直的站
for(;i < start.nextStation.length;i++)
{
if(start.nextStationFlag[i] == 0 )//flag == 0 表示有下一站可以走
{

int k = 0;
for(;k<start.nextStation[i].nextStationFlag.length;k++)
{
//下一站行初始化,把站周的直站(即start的下一站的再下一站)flag全部置成0,防止路重突
start.nextStation[i].nextStationFlag[k] = 0;

//下一站周的直站之前走的站比,如果一,就1,避免走回路
int l = 0;
for(;stationArr[l] != null;l++)
{
if(start.nextStation[i].nextStation[k].equals(stationArr[l]))
start.nextStation[i].nextStationFlag[k] = 1;
}
}
//start走向下一站,於是置1
start.nextStationFlag[i] = 1;
//到下一站路,此
funcStartToEnd(start.nextStation[i], end);
return;
}
}
}

//死路了,下一站都有了,只能往回跳了,此回溯
index -= 2;
for(i = 0;stationArr[i]!=null;i++)
{
if(stationArr[i].equals(end))
{
saveTheRoad(end);
SBVector.addElement(new StringBuffer(goToTheEnd));
count++;
}
}
stationArr[index+1] = null;
if(index>=0)
funcStartToEnd(stationArr[index], end);
return;
}

//把路存到StringBuffer中
public static void saveTheRoad(Station end)
{
goToTheEnd.delete(0, goToTheEnd.length());
int i = 0;
for(;stationArr[i]!=null;i++)
{
goToTheEnd.append(stationArr[i].stationName);
goToTheEnd.append(" - ");
if(stationArr[i].equals(end))break;
}
goToTheEnd.delete(goToTheEnd.length()-3, goToTheEnd.length() );
}

public static Station getStation(String stationName, Vector<Station> StationVector)
{
for(int i = 0; i<StationVector.size(); i++)
{
if(stationName.equals(StationVector.get(i).stationName))
return StationVector.get(i);
}
System.out.println(stationName+"不存在!");
return new Station("的站");
}
}
//主






package hignWay;


public class Station
{
public String stationName;//本站名
public Station nextStation[];//能直达的下一站数
public int nextStationFlag[];//能直达的下一站的flag,0能直达,1不能直达,一旦走过这条路,就设置为1

public Station(){}//构造函数

public Station(String name)
{
this.stationName = name;
}

//初始化周边站的方法(以及重载)
public void setNextStation(Station nextStation1)
{
this.nextStationFlag = new int[1];
this.nextStation = new Station[1];
this.nextStation[0] = nextStation1;
}

public void setNextStation(Station nextStation1, Station nextStation2)
{
this.nextStationFlag = new int[2];
this.nextStation = new Station[2];
this.nextStation[0] = nextStation1;
this.nextStation[1] = nextStation2;
}

public void setNextStation(Station nextStation1, Station nextStation2, Station nextStation3)
{
this.nextStationFlag = new int[3];
this.nextStation = new Station[3];
this.nextStation[0] = nextStation1;
this.nextStation[1] = nextStation2;
this.nextStation[2] = nextStation3;
}

public void setNextStation(Station nextStation1, Station nextStation2, Station nextStation3, Station nextStation4)
{
this.nextStationFlag = new int[4];
this.nextStation = new Station[4];
this.nextStation[0] = nextStation1;
this.nextStation[1] = nextStation2;
this.nextStation[2] = nextStation3;
this.nextStation[3] = nextStation4;
}

public void setNextStation(Station nextStation1, Station nextStation2, Station nextStation3, Station nextStation4, Station nextStation5)
{
this.nextStationFlag = new int[5];
this.nextStation = new Station[5];


this.nextStation[0] = nextStation1;
this.nextStation[1] = nextStation2;
this.nextStation[2] = nextStation3;
this.nextStation[3] = nextStation4;
this.nextStation[4] = nextStation5;
}
}


这个错误如何解决,求教大神,本人在线等

[解决办法]
是数组越界问题,你设置断点单步跟踪下,看下问题出在哪里,很好找的
[解决办法]
代码有点乱。。。
[解决办法]
Station start = getStation(args[0],StationVector);
象是这句,要求输入参数.
[解决办法]
at hignWay.Day0_HelloWorld.main(Day0_HelloWorld.java:54)
这行是哪行 ,你看看 54行

[解决办法]
你应该在 main方法的开始处写上
if (args.length != 2) { //判断是否有输入参数 ,即命令行中 java hignWay.Day0_HelloWorld AA BB,这样子才是正确的否则 跳出main
return ;
}


如果你是在eclipse或者myeclipse中运行,需要在
run -->run configrations-->java Application 选中你的运行时类名 ,然后在右边 arguments中输入你的两个参数
[解决办法]
共有3走法,下面列出最短的3
Road_0: 天堂 - 沼 - 山丘
Road_1: 天堂 - 草原 - 冥河 - 沼 - 山丘
Road_2: 天堂 - 草原 - 荒漠 - 洲 - 古墓 - - 冥河 - 沼 - 山丘

[解决办法]

Java code
package hignWay;import java.util.Vector;//主public class Day0_HelloWorld {    private static StringBuffer goToTheEnd = new StringBuffer(); // 存路(字符串,用于打印出)    private static Station stationArr[] = new Station[14]; // 存路(站的象,size自行整)    private static int index = 0; // stationArr[]的下    private static int count = 0; // 路    private static Vector<StringBuffer> SBVector = new Vector<StringBuffer>(); // 用来存各各的路径    private static Vector<Station> StationVector = new Vector<Station>();    // main函    public static void main(String[] args) {        if (args.length != 2){            System.out.println("请输入起始站点名称:如 java Day0_HelloWorld A B ");            return  ;        }        /*-------------------各站行初期化-------------------*/        /* 可以自行路行展,展了以後忘了初期化,加一站,周的都受影 */        /* 世界地 */        /* 天堂--沼--山丘 彩虹 */        /* | | | */        /* 草原--冥河----地-- */        /* | | */        /* 荒漠--洲--古墓--洞穴 */        Station tiantang = new Station("天堂");        StationVector.add(tiantang);        Station zhaoze = new Station("沼");        StationVector.add(zhaoze);        Station shanqiu = new Station("山丘");        StationVector.add(shanqiu);        Station caoyuan = new Station("草原");        StationVector.add(caoyuan);        Station minghe = new Station("冥河");        StationVector.add(minghe);        Station emeng = new Station("");        StationVector.add(emeng);        Station diyu = new Station("地");        StationVector.add(diyu);        Station lianyu = new Station("");        StationVector.add(lianyu);        Station caihong = new Station("彩虹");        StationVector.add(caihong);        Station huangmo = new Station("荒漠");        StationVector.add(huangmo);        Station lvzhou = new Station("洲");        StationVector.add(lvzhou);        Station gumu = new Station("古墓");        StationVector.add(gumu);        Station dongxue = new Station("洞穴");        StationVector.add(dongxue);        tiantang.setNextStation(zhaoze, caoyuan);        zhaoze.setNextStation(tiantang, shanqiu, minghe);        shanqiu.setNextStation(zhaoze);        caoyuan.setNextStation(tiantang, minghe, huangmo);        minghe.setNextStation(zhaoze, caoyuan, emeng);        emeng.setNextStation(minghe, diyu, gumu);        diyu.setNextStation(emeng, lianyu, caihong);        lianyu.setNextStation(diyu);        caihong.setNextStation(diyu);        huangmo.setNextStation(caoyuan, lvzhou);        lvzhou.setNextStation(huangmo, gumu);        gumu.setNextStation(lvzhou, emeng, dongxue);        dongxue.setNextStation(gumu);        /*-------------------各站行初期化-------------------*/        // 找路始Start to End        Station start = getStation(args[0], StationVector);        Station end = getStation(args[1], StationVector);        if (start.stationName.equals("的站")                || end.stationName.equals("的站"))            return;        Day0_HelloWorld.funcStartToEnd(start, end);        System.out.println("共有" + count + "走法,下面列出最短的"                + (count <= 3 ? count : 3) + "");        // 路Vector按升序排序并出前几路        int tempI = 0, tempJ;        StringBuffer tempSB = new StringBuffer();        for (; tempI < SBVector.size() - 1; tempI++) {            for (tempJ = tempI + 1; tempJ < SBVector.size(); tempJ++) {                if (SBVector.get(tempI).length() > SBVector.get(tempJ).length()) {                    tempSB = SBVector.get(tempI);                    SBVector.setElementAt(SBVector.get(tempJ), tempI);                    SBVector.setElementAt(tempSB, tempJ);                }            }        }        for (tempI = 0; tempI < (count <= 3 ? count : 3)/* SBVector.size() */; tempI++)            System.out.println("Road_" + tempI + ": " + SBVector.get(tempI));    }    public static void funcStartToEnd(Station start, Station end) {        stationArr[index++] = start; // 保存起始站        int i = 0;        if (!start.equals(end))// 起始站和束站不是同一站        {            // 向下一站出,逐遍周能直的站            for (; i < start.nextStation.length; i++) {                if (start.nextStationFlag[i] == 0)// flag == 0 表示有下一站可以走                {                    int k = 0;                    for (; k < start.nextStation[i].nextStationFlag.length; k++) {                        // 下一站行初始化,把站周的直站(即start的下一站的再下一站)flag全部置成0,防止路重突                        start.nextStation[i].nextStationFlag[k] = 0;                        // 下一站周的直站之前走的站比,如果一,就1,避免走回路                        int l = 0;                        for (; stationArr[l] != null; l++) {                            if (start.nextStation[i].nextStation[k]                                    .equals(stationArr[l]))                                start.nextStation[i].nextStationFlag[k] = 1;                        }                    }                    // start走向下一站,於是置1                    start.nextStationFlag[i] = 1;                    // 到下一站路,此                    funcStartToEnd(start.nextStation[i], end);                    return;                }            }        }        // 死路了,下一站都有了,只能往回跳了,此回溯        index -= 2;        for (i = 0; stationArr[i] != null; i++) {            if (stationArr[i].equals(end)) {                saveTheRoad(end);                SBVector.addElement(new StringBuffer(goToTheEnd));                count++;            }        }        stationArr[index + 1] = null;        if (index >= 0)            funcStartToEnd(stationArr[index], end);        return;    }    // 把路存到StringBuffer中    public static void saveTheRoad(Station end) {        goToTheEnd.delete(0, goToTheEnd.length());        int i = 0;        for (; stationArr[i] != null; i++) {            goToTheEnd.append(stationArr[i].stationName);            goToTheEnd.append(" - ");            if (stationArr[i].equals(end))                break;        }        goToTheEnd.delete(goToTheEnd.length() - 3, goToTheEnd.length());    }    public static Station getStation(String stationName,            Vector<Station> StationVector) {        for (int i = 0; i < StationVector.size(); i++) {            if (stationName.equals(StationVector.get(i).stationName))                return StationVector.get(i);        }        System.out.println(stationName + "不存在!");        return new Station("的站");    }} 

读书人网 >Java相关

热点推荐