0
我正在爲我的班級進行旅行推銷員計劃。我的老師給了我們一些文本文件,其中包含了某些城市的各種X,Y座標。我的程序正在爲擁有10,100和1000個城市的文件工作。出於某種原因,當我使用13,509個城市時,程序在對12,022個城市進行排序後會拋出索引超出界限的錯誤。我無法弄清楚問題所在,我無法在任何其他小城市名單(10個,100個或1000個城市)上重新創建問題。 我的第一個猜測是我的arrayIndex變量出了問題,但我不知道爲什麼它會停止在那個特定點上工作,以及爲什麼它不會在其他時間發生。任何幫助或建議,將不勝感激!索引出差旅行推銷員
public class TSP_GUI13509 extends JPanel
{
//ArrayList X, Y for the original City list
ArrayList<Double> xCoord = new ArrayList<>();
ArrayList<Double> yCoord = new ArrayList<>();
//ArrayList X, Y for the Final City list
ArrayList<Double> xCoordFinal = new ArrayList<>();
ArrayList<Double> yCoordFinal = new ArrayList<>();
//Initialize the shortestDist and distance variables
double shortestDist = Double.MAX_VALUE;
double distance = Double.MAX_VALUE;
public TSP_GUI13509(Container pane)
{
//sets n to the number of cities
int n = xCoord.size();
//Initialize the variables to hold the short City
double shortX = 0;
double shortY = 0;
//Number to place city in the correct position in final array
int mainCount = 0;
//Sets the first city to be the first point in the list
double x1 = xCoord.get(mainCount);
double y1 = yCoord.get(mainCount);
try
{
//loop through entire array of cities
while (xCoord.isEmpty() == false)
{
//ArrayIndex
int arrayIndex = 0;
//Initialize the second city to find the distance
double x2 = xCoord.get(arrayIndex);
double y2 = yCoord.get(arrayIndex);
//loop for 1 iteration of finding the shortest distance
for (int citiesLeft = xCoord.size(); citiesLeft > 0; citiesLeft--)
{
//Calculates the distance between city 1 and city 2
distance = Distance(x1, y1, x2, y2);
//distance has to be greater than 0
if (distance > 0)
{
//check for the shortest distance
if (distance < shortestDist)
{
shortestDist = distance;
shortX = x2;
shortY = y2;
}//end if (distance < shortestDist)
}//end if (distance > 0)
//increment the city index
arrayIndex++;
if (arrayIndex < xCoord.size())
{
x2 = xCoord.get(arrayIndex);
y2 = yCoord.get(arrayIndex);
}// end if (arrayIndex < xCoord.size())
}// end for - (int citiesLeft = n - 1; citiesLeft >= 0; citiesLeft--)
shortestDist = Double.MAX_VALUE;
//Adding the closest city to the final array and removing from the original array
if (xCoord.contains(shortX))
{
xCoordFinal.add(x1);
yCoordFinal.add(y1);
xCoord.remove(x1);
yCoord.remove(y1);
}//end if (xCoord.contains(shortX))
if (arrayIndex >= xCoord.size())
{
x1 = shortX;
y1 = shortY;
//throw new IndexOutOfBoundsException("Final = " + xCoordFinal.size() + "\nReg = " + xCoord.size());
}//end if (arrayIndex >= xCoord.size())
//increment the city index
if (arrayIndex == xCoord.size())
{
arrayIndex++;
}// end if (arrayIndex < xCoord.size())
System.out.println("\nArrayIndex" + arrayIndex);
System.out.println("\nCities left " + xCoord.size());
System.out.println("\nCities complete " + xCoordFinal.size());
}//end while (int mainCount = 0; mainCount < n; mainCount++)
}//end try
catch (IndexOutOfBoundsException e)
{
System.out.println("This is your problem: " + e.getMessage() +
"\nHere is where it happened:\n");
e.printStackTrace();
}
}//End TSP_GUI container pane
//Distance method. Finds the distance between pt 1 and pt 2
public double Distance(double x1, double y1, double x2, double y2)
{
distance = Math.sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
return distance;
}//end Distance method
}//end Class TSP_GUI
這裏是的printStackTrace
java.lang.IndexOutOfBoundsException: Index: 12022, Size: 12022
at java.util.ArrayList.rangeCheck(ArrayList.java:653)
at java.util.ArrayList.get(ArrayList.java:429)
at tsp.TSP_GUI13509.<init>(TSP_GUI13509.java:117)
at tsp.TSPScreen13509.<init>(TSPScreen13509.java:20)
at tsp.TSP$4.actionPerformed(TSP.java:158)
at javax.swing.AbstractButton.fireActionPerformed(AbstractButton.java:2022)
at javax.swing.AbstractButton$Handler.actionPerformed(AbstractButton.java:2346)
at javax.swing.DefaultButtonModel.fireActionPerformed(DefaultButtonModel.java:402)
at javax.swing.DefaultButtonModel.setPressed(DefaultButtonModel.java:259)
at javax.swing.plaf.basic.BasicButtonListener.mouseReleased(BasicButtonListener.java:252)
at java.awt.Component.processMouseEvent(Component.java:6525)
at javax.swing.JComponent.processMouseEvent(JComponent.java:3322)
at java.awt.Component.processEvent(Component.java:6290)
at java.awt.Container.processEvent(Container.java:2234)
at java.awt.Component.dispatchEventImpl(Component.java:4881)
at java.awt.Container.dispatchEventImpl(Container.java:2292)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.LightweightDispatcher.retargetMouseEvent(Container.java:4898)
at java.awt.LightweightDispatcher.processMouseEvent(Container.java:4533)
at java.awt.LightweightDispatcher.dispatchEvent(Container.java:4462)
at java.awt.Container.dispatchEventImpl(Container.java:2278)
at java.awt.Window.dispatchEventImpl(Window.java:2739)
at java.awt.Component.dispatchEvent(Component.java:4703)
at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:751)
at java.awt.EventQueue.access$500(EventQueue.java:97)
at java.awt.EventQueue$3.run(EventQueue.java:702)
at java.awt.EventQueue$3.run(EventQueue.java:696)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:86)
at java.awt.EventQueue$4.run(EventQueue.java:724)
at java.awt.EventQueue$4.run(EventQueue.java:722)
at java.security.AccessController.doPrivileged(Native Method)
at java.security.ProtectionDomain$1.doIntersectionPrivilege(ProtectionDomain.java:75)
at java.awt.EventQueue.dispatchEvent(EventQueue.java:721)
at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)
請編輯您的問題以包含異常堆棧跟蹤並指出引發異常的行。另請閱讀[*'如何創建一個最小,完整和可驗證的示例'*](http://stackoverflow.com/help/mcve),並認真考慮將您的代碼縮減爲這樣一個示例。 – Radiodef 2015-04-02 02:00:25