Java 对二维数组排序会在某些值中产生零
我希望有人能很容易地指出我做了哪些错误的选择,使这个数组排序的东西无法工作 我有一个数据集,我希望排序,这样我可以把一个函数的值绘制到屏幕上,但我需要这些排序的时间值从最小到最大。一旦我将树映射解析为二维数组,然后对值进行排序,排序过程就会删除一些值并用零替换它们 下面是完整的代码,下面是我使用的三个集合之间的一些结果: 非常感谢您的帮助…谢谢 输出 地图6091097.15385107.53 2D 0.0,[0.0,0.0] 全部[0.0,0.0,0.0] 地图6761094.65385112.53 2D 0.0,[0.0,0.0] 全部[0.0,0.0,0.0] 地图5921092.15385107.53 2D 0.0,[0.0,0.0] 全部[0.0,0.0,0.0] 地图7521124.65385102.53 2D 1154.0[1067.15385127.53] 全部[1154.01067.15385127.53] 地图8611127.15385107.53 2D 1154.0[1067.15385127.53] 全部[1154.01067.15385127.53] 地图10371127.15385117.53 2D 1154.0[1067.15385127.53] 全部[1154.01067.15385127.53] 地图12131127.15385127.53 2D 1213.0[1127.15385127.53] 全部[1213.01127.15385127.53]Java 对二维数组排序会在某些值中产生零,java,arrays,sorting,multidimensional-array,comparator,Java,Arrays,Sorting,Multidimensional Array,Comparator,我希望有人能很容易地指出我做了哪些错误的选择,使这个数组排序的东西无法工作 我有一个数据集,我希望排序,这样我可以把一个函数的值绘制到屏幕上,但我需要这些排序的时间值从最小到最大。一旦我将树映射解析为二维数组,然后对值进行排序,排序过程就会删除一些值并用零替换它们 下面是完整的代码,下面是我使用的三个集合之间的一些结果: 非常感谢您的帮助…谢谢 输出 地图6091097.15385107.53 2D 0.0,[0.0,0.0] 全部[0.0,0.0,0.0] 地图6761094.65385112
在使用Arrays.sort对其进行排序之前,请完全初始化timeLocation\u 2DArray,或使用Arrays.sort…,0,行+1,新。。。将排序限制为数组的初始化部分。否则,具有正时间的数据将被排序到数组的末尾。您的数组在使用新的双精度[…][3]分配后包含所有零。感谢您的及时响应。这种方法现在正在发挥作用。我将排序和排序后的位置放入第一个循环之外的数组中,并创建了另一个循环,现在已经解决了排序问题。干杯。
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.
}
}
}
}