检查订单2d数组问题

时间:2016-02-08 10:07:22

标签: java arrays eclipse sorting

我有一个XML页面,其中有4列数据。需要对这4列进行排序,首先是第1列,然后是第2列,依此类推。在前两列中有文本,第三列包含3digit编号,最后一列包含double或bigdecimal。我从XML中获取数据并放入一个二维数组来检查排序是否正确。为了测试我编写的代码以检查列中的排序,我创建了一个小的2d数组: String [] [] xml = new String [10] [4];

After Home 182 5.1

After Home 182 5.9

After Home 182 5.10

离开之前150 4.6

犹他州之前852 3.8

在锡安110 110.99之前

在锡安110 110.100之前

Control Everywhere 475 65.1

Control Remote 369 38.6

Control Voip 598 44.9

这是我用来检查列的排序的整个代码段(可以复制粘贴到Eclipse):

  String [][] xml = new String [10][4];

  //column 1
  xml[0][0]="After";
  xml[1][0]="After";
  xml[2][0]="After";
  xml[3][0]="Before";
  xml[4][0]="Before";
  xml[5][0]="Before";
  xml[6][0]="Before";
  xml[7][0]="Control";
  xml[8][0]="Control";
  xml[9][0]="Control";
  //column 2
  xml[0][1]="Home";
  xml[1][1]="Home";
  xml[2][1]="Home";
  xml[3][1]="Away";
  xml[4][1]="Utah";
  xml[5][1]="Zion";
  xml[6][1]="Zion";
  xml[7][1]="Everywhere";
  xml[8][1]="Remote";
  xml[9][1]="Voip";
  //column 3
  xml[0][2]="182";
  xml[1][2]="182";
  xml[2][2]="182";
  xml[3][2]="150";
  xml[4][2]="852";
  xml[5][2]="110";
  xml[6][2]="110";
  xml[7][2]="475";
  xml[8][2]="369";
  xml[9][2]="598";
  //column 4
  xml[0][3]="5.1";
  xml[1][3]="5.9";
  xml[2][3]="5.10";
  xml[3][3]="4.6";
  xml[4][3]="3.8";
  xml[5][3]="110.99";
  xml[6][3]="110.100";
  xml[7][3]="65.1";
  xml[8][3]="38.6";
  xml[9][3]="44.9";

  for (int p=1;p<xml.length;p++){

        int column1 = xml[(p)][0].compareTo(xml[(p-1)][0]);
        boolean sortcolumn1 = column1>=0; //If value 0 then cells match (boolean True). if value equals 1 then value in second cell  is further in the alphabet then the cell before it(boolean True). if value equals -1 then value in second cell is earlier in the alphabet then the cell before it(boolean False).
        if(sortcolumn1==false){//Check on 1st column
            System.out.println("1st Colum error: " +xml[(p)][0]+ " is before " +xml[(p-1)][0]);
        }

        //Checking the data in the 2nd column should only be done when the data in the two adjacent cells of the first column is equal.
            if (column1 == 0) {
                int column2 = xml[(p)][1].compareTo(xml[(p - 1)][1]);
                if (column2 < 0) {// Check on 2nd column
                    System.out.println("Column2 error: " + xml[(p)][1] + " is before " + xml[(p - 1)][1]);
                }
                if (column2 == 0) {
                    if (xml[(p - 1)][2] != "" && xml[(p)][2] != "") {
                        try {
                            Integer a = Integer.parseInt(xml[(p)][2]);
                            Integer b = Integer.parseInt(xml[(p - 1)][2]);
                            Integer column3 =  a -b; 

                            if (column3 < 0) {// Check on 3rd column
                                System.out.println("Calculation column3: "+a+"-"+b+"=" + column3);
                                System.out.println("Column 3 error: " + xml[(p)][2] + " is lower then " + xml[(p - 1)][2]);
                            }
                        } catch (NumberFormatException e) {
                        }
                        int column3 = xml[(p)][2].compareTo(xml[(p - 1)][2]);
                        if (column3 == 0) {
                            if (xml[(p - 1)][3] != "" && xml[(p)][3] != "") {
                                try {
                                    BigDecimal a = new BigDecimal(xml[(p)][3]);
                                    BigDecimal b = new BigDecimal(xml[(p - 1)][3]);
                                    BigDecimal column4 = a.subtract(b);


                                    if (column4.compareTo(BigDecimal.ZERO) < 0) {// Check on 4th column
                                        System.out.println("Calculation column4: "  +a+"-"+b+"="+ column4);
                                        System.out.println("Column 4 error: " + xml[(p)][3] + " is lower then " + xml[(p - 1)][3]);
                                    }
                                } catch (NumberFormatException e) {
                                }
                            }
                        }
                    }
                }
            }
        }
    }

代码似乎工作正常,但我认为5.9低于5.10和110.99低于110.100代码不同意。这是我得到的打印结果:

Calculation column4: 5.10-5.9=-0.80
Column 4 error: 5.10 is lower then 5.9
Calculation column4: 110.100-110.99=-0.890
Column 4 error: 110.100 is lower then 110.99

我尝试过double而不是BigDecimal但是5.10只显示为5.1。我认为这是问题,但我无法弄清楚如何解决它。我想通过使用the将最终列分成两部分是可能的。作为一个分隔符和两列的排序,但这会带来它自己的一系列问题。

1 个答案:

答案 0 :(得分:0)

您可以尝试使用POJO而不是2d数组。

public class Test implements Comparable<Test> {

private String field1;

private String field2;

private Integer field3;

private Double field4;

@Override
public int compareTo(Test o) {
    // There is your sorting algorythm
    return 0;
}

然后在处理Test类列表中的数据之后,你只需使用:

进行排序
    final List<Test> testList = new ArrayList<Test>();
    Collections.sort(testList);

希望有所帮助。