【发布时间】:2015-06-06 20:40:37
【问题描述】:
我正在为我的班级制定一个旅行推销员计划。我的老师给了我们一些文本文件,其中包含某些城市的各种 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)
【问题讨论】:
-
请编辑您的问题以包含异常堆栈跟踪并指出引发异常的行。另请阅读'How to create a Minimal, Complete, and Verifiable example' 并认真考虑将您的代码简化为这样的示例。
标签: java arraylist while-loop indexoutofboundsexception