日期:2014-05-20  浏览次数:21043 次

数组越界问题
程序一点运行就出错误,错误如下:
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 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)
{
/*-------------------對各個站臺進行初期化-------------------*/
/*可以自行對路線圖進行擴展,擴展了以後別忘了初期化,毎加一個站點,周邊的都會受影響*/
/* 世界地圖 */
/* 天堂--沼澤--山丘 彩虹 */
/* | | | */
/* 草原--冥河--惡夢--地獄--煉獄 */
/* | | */
/* 荒漠--綠洲--古墓--洞穴 */
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; //保存起始站