旅行推销员的指数超出界限

时间:2015-04-02 01:45:32

标签: java arraylist while-loop indexoutofboundsexception

我正在为我班级的旅行推销员计划。我的老师给了我们一些文本文件,其中包含某些城市的各种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)

1 个答案:

答案 0 :(得分:0)

只是更新。我终于找到了问题。这与我的循环无关。这是我的文本文件中的重复问题。当我找到下一个最近的城市时,我将其添加到我的最终城市列表中,并将其从主要城市列表中删除。有些城市有重复的x值,所以当我删除一个重复的x值时,它会删除它两次,这搞砸了我的计数。