对2维数组进行排序会在某些值中生成零

时间:2013-12-21 15:40:01

标签: java arrays sorting multidimensional-array comparator

我希望有人可以很容易地指出我为使这个数组排序不起作用而做出的错误选择。

我有一个我希望排序的数据集,所以我可以将值放在轮廓(行进方块)函数中绘制到屏幕上,但是我需要将它们按照从最小到最大的时间值进行排序。一旦我将TreeMap解析为二维数组然后对值进行排序,SORTING过程就会删除一些值并用零替换它们。

这是完整的代码,下面是我使用的三个集合之间的一些结果:

非常感谢任何帮助...谢谢

        public void paintContours(Graphics2D g2, World world, Rectangle2D bounds, Rectangle2D canvas, double scale) {

        //Class to store the three attributes for mapping.
        class Triple {  
            int t;  double x;   double y;   
            public Triple(int time, double xLoc, double yLoc) { 
                t = time;   
                x = xLoc;   
                y = yLoc;  
                }   
            }
        //Set attributes of the Renderer
        g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
        //Do for the Pattern in the World 
        for(Pattern p: world.getPatternList().values()) {
            //Do if the pattern has at least 1 initiation point
            if(!p.getiPList().isEmpty()) {
                //Create a new Triple
                Triple store;
                //Create a new TreeMap to store all Triples
                TreeMap<Integer, Triple> map = new TreeMap<Integer, Triple>();
                //Set a counting key
                int key = 0;
                //Set the range for the rendered result
                double xmin = p.getBounds().getMinX();
                double ymin = p.getBounds().getMinY();
                double xmax = p.getBounds().getMaxX();
                double ymax = p.getBounds().getMaxY();
                //Do for all surface connectors in the pattern
                for(SurfaceConnector sc: p.getSurfaceList().values()) {
                    //Put values to map in the Triple
                    store = new Triple(sc.getTime(),sc.getTo().getEasting(),sc.getTo().getNorthing());
                    //Put the Triple in the map
                    map.put(key, store);
                    //Increment the counting key
                    key++;
                }       
                //Create a 1 dimensional array for storing time
                double [] time_1DArray = new double[map.size()];
                //Create a 2 dimentional array for storing locations
                double [] [] location_2DArray = new double[map.size()][2];
                //Create a 2 dimentional array for storing time, x, y so they can be sorted in ascending time order.
                double [] [] timeLocation_2DArray = new double[map.size()][3];
                //Do if there are values in the Treemap
                if (!(map.values().isEmpty())) {
                    //Loop through the rows
                    for (int row = 0; row < time_1DArray.length; row++) {
                        //Assign the values in the Triple TreeMap to the doubles 
                        double tt = map.get(row).t;
                        double xx = map.get(row).x;
                        double yy = map.get(row).y;
                        //Loop through the columns
                        for(int column = 0; column<3; column++) {
                            //Put the time in the first column
                            if(column == 0)         {timeLocation_2DArray[row][column] = tt;}
                            //Put the x in the second column
                            else if(column == 1)    {timeLocation_2DArray[row][column] = xx;}
                            //Put the y in the third column
                            else if(column == 2)    {timeLocation_2DArray[row][column] = yy;}
                        }

                        //Sort the array in order ascending from 0 to ∞ - based on time order
                        Arrays.sort(timeLocation_2DArray, new Comparator<double[]>() {
                            public int compare(double[] o1, double[] o2) {
                                //                                      return Double.compare(o1[0], o2[0]);
                                if (o1[0] > o2[0])      return 1;    // o1 comes after o2
                                else if (o1[0] < o2[0]) return -1;   // o1 comes before o2
                                else {                  return 0;}
                            }
                        });
                        //place sorted values in to the appropriate arrays.
                        for(int column = 0; column < 3; column++) {
                            //Time goes in the time array
                            if(column == 0) {
                                time_1DArray[row] = (timeLocation_2DArray[row][column]);
                            }
                            //X values go in the first column of the Location Array
                            else if(column == 1) {
                                location_2DArray[row][column-1] = (timeLocation_2DArray[row][column]);
                            }
                            //Y values go in the second column of the Location Array
                            else if (column == 2) {
                                location_2DArray[row][column-1] =  (timeLocation_2DArray[row][column]); 
                            }
                        }
                        //Print the Arrays for confirmation and checking - Comment out when working
                        System.out.println("Map "+ map.get(row).t +","+ map.get(row).x +","+map.get(row).y);
                        System.out.println("2D " +time_1DArray[row] +","+Arrays.toString(location_2DArray[row]));
                        System.out.println("All "+Arrays.toString(timeLocation_2DArray[row]));
                    }
                }
                //Create a new marching square algorithm
                MarchingSquares marchingSquares = new MarchingSquares();
                //Create the iso lines from the algorithm usin the above sorted arrays.
                GeneralPath[] isolines = marchingSquares.mkIsos(location_2DArray, time_1DArray); 
                //PRNTLN For checking the ascii representation of the IsoLines
                //System.out.println(marchingSquares.asciiPrintContours(array2d, array));               
                // Convert isos from array coords to world UTM coords.
//              AffineTransform xf = new AffineTransform();
                //
//              xf.scale((canvas.getWidth())/(p.getBounds().getWidth()),(canvas.getHeight())/(p.getBounds().getHeight()));

//              xf.translate(ymin, xmin);
                //          xf.translate(-1, -1); // Because MxN data was padded to (M+2)x(N+2).
                for (int i = 0; i < isolines.length; i++) {

//                  isolines[i].transform(xf); // Permanent mapping to world coords.

                    Shape iso = (isolines[i]); // Remapped every pan & zoom.
                    g2.setColor(Color.BLACK);
                    g2.setStroke(LINE_250);

                    g2.draw(iso); // Color iso.
                    g2.setStroke(DASH_200_008);
                    g2.setColor(Color.YELLOW);

                    g2.draw(iso); // Outline iso.
                }

            }
        }
    }

输出 地图609,1097.1538,5107.53 2D 0.0,[0.0,0.0] 全部[0.0,0.0,0.0] 地图676,1094.6538,5112.53 2D 0.0,[0.0,0.0] 全部[0.0,0.0,0.0] 地图592,1092.1538,5107.53 2D 0.0,[0.0,0.0] 全部[0.0,0.0,0.0] 地图752,1124.6538,5102.53 2D 1154.0,[1067.1538,5127.53] 全部[1154.0,1067.1538,5127.53] 地图861,1127.1538,5107.53 2D 1154.0,[1067.1538,5127.53] 全部[1154.0,1067.1538,5127.53] 地图1037,1127.1538,5117.53 2D 1154.0,[1067.1538,5127.53] 全部[1154.0,1067.1538,5127.53] 地图1213,1127.1538,5127.53 2D 1213.0,[1127.1538,5127.53] 全部[1213.0,1127.1538,5127.53]

Picture of Data Set with dysfunctional contour in top right corner (Yellow and Black)

0 个答案:

没有答案