数组越界问题
程序一点运行就出错误,错误如下:
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: 天堂 - 草原 - 荒漠 - 綠洲 - 古墓 - 惡夢 - 冥河 - 沼澤 - 山丘
[解决办法]
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("錯誤的車站"); }}