【问题标题】:How can I put alphabetically a double dimensional table?如何按字母顺序放置一个二维表?
【发布时间】:2020-10-15 22:30:02
【问题描述】:

我有一个二维表,在他的第一行有字母,每个字母下面都有数字。我想按字母顺序排列第一行,但是对于每个改变它的位置的字母,我希望下面的数字一起移动。

例如我有这张桌子:

C O R T I N A
1 1 0 0 3 4 0
4 3 3 1 2 1 3
2 1 2 1 2 2 0
0 2 3 0 2 0 4
2 3 3 4 0 4 3
2 

结果必须是:

A C I N O R T
0 1 3 4 1 0 0
3 4 2 1 3 3 1
0 2 2 2 1 2 1
4 0 2 0 2 3 0
3 2 0 4 3 3 4
  2

我已经尝试过这是我的代码,但它不起作用。在 cmets 中是我认为我做到了但它不起作用的整个代码。然后我尝试只更改第一行,但仍然失败。有人可以帮我吗?

                for (int i=0; i<table2.length; i++) {
                for (int j=0; j<table2[0].length; j++) {
                    if (i==0 && ((int)table2[i][i]>(int)table2[i][j])) {
                        char d = table2[i][j];
                        table2[i][j]=table2[i][i];
                        table2[i][i]=(char)d;
//                          char [] help = new char [table2[j].length];
//                          for (int k=0; k<help.length; k++) {
//                              help[k]=table2[i+k][j];
//                              //System.out.print(help[k]);
//                          }
//
//                          for (int u=0; u<table2.length; u++) {
//                              for (int p=0; p<table2[0].length; p++) {
//                                  if (u==0 && (int)table2[u][u]>(int)table2[u][p]) {
//                                      table2[u][p] = table2[u][u];
//
//                                      for (int k=0; k<help.length; k++) {
//                                          table2[u][u] = help[k];
//                                      }
//                                  }
//                              }
//                          }

                    }



                    System.out.print((char)table2[i][j] + " ");
                    if (j==userKeyWord.length()-1)
                        System.out.println();
                }
            }

我忘了提到最后的 userKeyWord 是用户为下面要打印的数字提供的单词。 (ofc 只是代码的一部分)

【问题讨论】:

  • 为了确定,C 列有一个额外的数字,对吗?

标签: java arrays alphabetical


【解决方案1】:

我首先根据字母值对第一行的索引进行排序。然后我使用这些索引重新排列其他行以反映相同的位置。为了满足最后的唯一值,我添加了 -1 的虚拟值来填充未使用的数组位置。这使得矩阵更统一,更易于使用。

int[][] mat = { { 'C', 'O', 'R', 'T', 'I', 'N', 'A' },
        { 1, 1, 0, 0, 3, 4, 0 },
        { 4, 3, 3, 1, 2, 1, 3 },
        { 2, 1, 2, 1, 2, 2, 0 },
        { 0, 2, 3, 0, 2, 0, 4 },
        { 2, 3, 3, 4, 0, 4, 3 },
        { 2,-1,-1,-1,-1,-1,-1 } };

获取第一行并根据它对索引进行排序。

int[] first = mat[0];

int[] indices = IntStream.range(0, first.length).boxed()
        .sorted((a, b) -> Integer.compare(mat[0][a],
                mat[0][b]))
        .mapToInt(Integer::intValue).toArray();

现在使用这些索引来构建一个新矩阵

for (int row = 0; row < mat.length; row++) {
    int[] temp = mat[row];
    mat[row] = Arrays.stream(indices).map(i -> temp[i])
            .toArray();
}

然后打印出来


for (int i = 0; i < mat[0].length; i++) {
    System.out.printf(" %c ", mat[0][i]);
}
System.out.println();

for (int row = 1; row < mat.length; row++) {
    for (int col = 0; col < mat[row].length; col++) {
        int v = mat[row][col];
        System.out.printf("%2s ", v < 0 ? "" : v);
    }
    System.out.println();
}

打印

 A  C  I  N  O  R  T 
 0  1  3  4  1  0  0 
 3  4  2  1  3  3  1 
 0  2  2  2  1  2  1 
 4  0  2  0  2  3  0 
 3  2  0  4  3  3  4 
    2                

【讨论】:

    【解决方案2】:

    我不能 100% 确定您的代码为什么不起作用,但我的处理方式略有不同。这就是我所做的。

    private static final char[][] unsortedArray = {{'C', 'O', 'R', 'T', 'I', 'N', 'A'},
                                                    {1, 1, 0, 0, 3, 4, 0},
                                                    {4, 3, 3, 1, 2, 1, 3},
                                                    {2, 1, 2, 1, 2, 2, 0},
                                                    {0, 2, 3, 0, 2, 0, 4},
                                                    {2, 3, 3, 4, 0, 4, 3}};
    
    //Object to hold original index and key
    private static class IndexTuple {
        protected char key;
        protected int unsortedIndex;
    
        public IndexTuple(char key, int unsortedIndex) {
            this.key = key;
            this.unsortedIndex = unsortedIndex;
        }
    }
    
    //Comparator to let collections sort for you
    private static class TupleComparator implements Comparator<IndexTuple> {
    
        @Override
        public int compare(IndexTuple o1, IndexTuple o2) {
            return o1.key - o2.key;
        }
    }
    
    public static char[][] sortTable(char[][] unsortedTable) {
        //Create new array to copy result to
        char[][] sortedTable = new char[unsortedTable.length][unsortedTable[0].length];
    
        //Copy result to List of custom objects
        ArrayList<IndexTuple> tupleList = new ArrayList<>();
        for (int i = 0; i < unsortedTable[0].length; i++) {
            tupleList.add(new IndexTuple(unsortedTable[0][i], i));
        }
    
        //sort list
        tupleList.sort(new TupleComparator());
    
        //Using original unsorted index and new sorted index set all the values in correct order
        for (int i = 0; i < unsortedTable.length; i++) {
            for (int j = 0; j < tupleList.size(); j++) {
                sortedTable[i][j] = unsortedTable[i][tupleList.get(j).unsortedIndex];
            }
    
        }
        return sortedTable;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-07-03
      • 1970-01-01
      • 2017-04-30
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-02-05
      相关资源
      最近更新 更多