如何按字母顺序排列双维表格?

huangapple go评论95阅读模式
英文:

How can I put alphabetically a double dimensional table?

问题

以下是翻译好的代码部分:

  1. for (int i = 0; i < table2.length; i++) {
  2. for (int j = 0; j < table2[0].length; j++) {
  3. if (i == 0 && ((int) table2[i][i] > (int) table2[i][j])) {
  4. char d = table2[i][j];
  5. table2[i][j] = table2[i][i];
  6. table2[i][i] = (char) d;
  7. }
  8. System.out.print((char) table2[i][j] + " ");
  9. if (j == userKeyWord.length() - 1)
  10. System.out.println();
  11. }
  12. }

注意:这只是你提供的代码的翻译部分,可能仍然需要检查和调试,以确保其正确性和完整性。另外,你提到的 userKeyWord 部分在你提供的代码片段中并未包含在内,所以需要在实际代码中补充相关部分。

英文:

I have a double dimensional table which on his first line has letters and below every letter there are numbers. I want to put the first line alphabetically but for every letter that change it's place I want the numbers below to move together.

For example I have this table:

  1. C O R T I N A
  2. 1 1 0 0 3 4 0
  3. 4 3 3 1 2 1 3
  4. 2 1 2 1 2 2 0
  5. 0 2 3 0 2 0 4
  6. 2 3 3 4 0 4 3
  7. 2

and the result must be:

  1. A C I N O R T
  2. 0 1 3 4 1 0 0
  3. 3 4 2 1 3 3 1
  4. 0 2 2 2 1 2 1
  5. 4 0 2 0 2 3 0
  6. 3 2 0 4 3 3 4
  7. 2

I have make a try here is my code but it doesn't work. In comments is the whole code I thought I did it but it's not working. Then I tried to change just the first line and I still failed. Can someone help me?

  1. for (int i=0; i&lt;table2.length; i++) {
  2. for (int j=0; j&lt;table2[0].length; j++) {
  3. if (i==0 &amp;&amp; ((int)table2[i][i]&gt;(int)table2[i][j])) {
  4. char d = table2[i][j];
  5. table2[i][j]=table2[i][i];
  6. table2[i][i]=(char)d;
  7. // char [] help = new char [table2[j].length];
  8. // for (int k=0; k&lt;help.length; k++) {
  9. // help[k]=table2[i+k][j];
  10. // //System.out.print(help[k]);
  11. // }
  12. //
  13. // for (int u=0; u&lt;table2.length; u++) {
  14. // for (int p=0; p&lt;table2[0].length; p++) {
  15. // if (u==0 &amp;&amp; (int)table2[u][u]&gt;(int)table2[u]

    ) {

  16. // table2[u]

    = table2[u][u];

  17. //

  18. // for (int k=0; k&lt;help.length; k++) {

  19. // table2[u][u] = help[k];

  20. // }

  21. // }

  22. // }

  23. // }

  24. }

  25. System.out.print((char)table2[i][j] + &quot; &quot;);

  26. if (j==userKeyWord.length()-1)

  27. System.out.println();

  28. }

  29. }

I forgot to mention the userKeyWord at the end is the word the user gives for the numbers to print below. (ofc it's just a part of the code)

答案1

得分: 1

我首先通过对第一行的字母值进行排序来排序第一行的索引。然后,我使用这些索引来重新排列其他行,以反映相同的位置。为了迎合末尾的单独值,我添加了值为-1的虚拟值来填充未使用的数组位置。这使得矩阵更加均匀且更易于处理。

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

获取第一行并基于其进行索引排序。

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

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

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

然后打印它。

  1. for (int i = 0; i < mat[0].length; i++) {
  2. System.out.printf(" %c ", mat[0][i]);
  3. }
  4. System.out.println();
  5. for (int row = 1; row < mat.length; row++) {
  6. for (int col = 0; col < mat[row].length; col++) {
  7. int v = mat[row][col];
  8. System.out.printf("%2s ", v < 0 ? "" : v);
  9. }
  10. System.out.println();
  11. }

打印结果为:

  1. A C I N O R T
  2. 0 1 3 4 1 0 0
  3. 3 4 2 1 3 3 1
  4. 0 2 2 2 1 2 1
  5. 4 0 2 0 2 3 0
  6. 3 2 0 4 3 3 4
  7. 2
英文:

I did this by first sorting the indices of the first row based on the letter value. Then I used those indices to rearrange the other rows to reflect the same position. To cater to the lone value at the end I added dummy values of -1 to fill the unused array locations. This makes the matrix more uniform and easier to work with.

  1. int[][] mat = { { &#39;C&#39;, &#39;O&#39;, &#39;R&#39;, &#39;T&#39;, &#39;I&#39;, &#39;N&#39;, &#39;A&#39; },
  2. { 1, 1, 0, 0, 3, 4, 0 },
  3. { 4, 3, 3, 1, 2, 1, 3 },
  4. { 2, 1, 2, 1, 2, 2, 0 },
  5. { 0, 2, 3, 0, 2, 0, 4 },
  6. { 2, 3, 3, 4, 0, 4, 3 },
  7. { 2,-1,-1,-1,-1,-1,-1 } };

Get the first row and sort the indices based on that.

  1. int[] first = mat[0];
  2. int[] indices = IntStream.range(0, first.length).boxed()
  3. .sorted((a, b) -&gt; Integer.compare(mat[0][a],
  4. mat[0][b]))
  5. .mapToInt(Integer::intValue).toArray();

Now use those indices to build a new matrix

  1. for (int row = 0; row &lt; mat.length; row++) {
  2. int[] temp = mat[row];
  3. mat[row] = Arrays.stream(indices).map(i -&gt; temp[i])
  4. .toArray();
  5. }

And then print it

  1. for (int i = 0; i &lt; mat[0].length; i++) {
  2. System.out.printf(&quot; %c &quot;, mat[0][i]);
  3. }
  4. System.out.println();
  5. for (int row = 1; row &lt; mat.length; row++) {
  6. for (int col = 0; col &lt; mat[row].length; col++) {
  7. int v = mat[row][col];
  8. System.out.printf(&quot;%2s &quot;, v &lt; 0 ? &quot;&quot; : v);
  9. }
  10. System.out.println();
  11. }

Prints

  1. A C I N O R T
  2. 0 1 3 4 1 0 0
  3. 3 4 2 1 3 3 1
  4. 0 2 2 2 1 2 1
  5. 4 0 2 0 2 3 0
  6. 3 2 0 4 3 3 4
  7. 2
  8. </details>
  9. # 答案2
  10. **得分**: 1
  11. 我不确定为什么你的代码不起作用,但我以稍微不同的方式来处理它。这是我的做法。
  12. ```java
  13. private static final char[][] unsortedArray = {{'C', 'O', 'R', 'T', 'I', 'N', 'A'},
  14. {1, 1, 0, 0, 3, 4, 0},
  15. {4, 3, 3, 1, 2, 1, 3},
  16. {2, 1, 2, 1, 2, 2, 0},
  17. {0, 2, 3, 0, 2, 0, 4},
  18. {2, 3, 3, 4, 0, 4, 3}};
  19. // 保存原始索引和键的对象
  20. private static class IndexTuple {
  21. protected char key;
  22. protected int unsortedIndex;
  23. public IndexTuple(char key, int unsortedIndex) {
  24. this.key = key;
  25. this.unsortedIndex = unsortedIndex;
  26. }
  27. }
  28. // 比较器,用于让集合进行排序
  29. private static class TupleComparator implements Comparator<IndexTuple> {
  30. @Override
  31. public int compare(IndexTuple o1, IndexTuple o2) {
  32. return o1.key - o2.key;
  33. }
  34. }
  35. public static char[][] sortTable(char[][] unsortedTable) {
  36. // 创建新数组以复制结果
  37. char[][] sortedTable = new char[unsortedTable.length][unsortedTable[0].length];
  38. // 将结果复制到自定义对象的列表中
  39. ArrayList<IndexTuple> tupleList = new ArrayList<>();
  40. for (int i = 0; i < unsortedTable[0].length; i++) {
  41. tupleList.add(new IndexTuple(unsortedTable[0][i], i));
  42. }
  43. // 排序列表
  44. tupleList.sort(new TupleComparator());
  45. // 使用原始未排序索引和新排序索引,将所有值设置为正确的顺序
  46. for (int i = 0; i < unsortedTable.length; i++) {
  47. for (int j = 0; j < tupleList.size(); j++) {
  48. sortedTable[i][j] = unsortedTable[i][tupleList.get(j).unsortedIndex];
  49. }
  50. }
  51. return sortedTable;
  52. }
英文:

I am not 100% sure why your code isn't working, but I approached it a little bit differently. Here is what I did.

  1. private static final char[][] unsortedArray = {{&#39;C&#39;, &#39;O&#39;, &#39;R&#39;, &#39;T&#39;, &#39;I&#39;, &#39;N&#39;, &#39;A&#39;},
  2. {1, 1, 0, 0, 3, 4, 0},
  3. {4, 3, 3, 1, 2, 1, 3},
  4. {2, 1, 2, 1, 2, 2, 0},
  5. {0, 2, 3, 0, 2, 0, 4},
  6. {2, 3, 3, 4, 0, 4, 3}};
  7. //Object to hold original index and key
  8. private static class IndexTuple {
  9. protected char key;
  10. protected int unsortedIndex;
  11. public IndexTuple(char key, int unsortedIndex) {
  12. this.key = key;
  13. this.unsortedIndex = unsortedIndex;
  14. }
  15. }
  16. //Comparator to let collections sort for you
  17. private static class TupleComparator implements Comparator&lt;IndexTuple&gt; {
  18. @Override
  19. public int compare(IndexTuple o1, IndexTuple o2) {
  20. return o1.key - o2.key;
  21. }
  22. }
  23. public static char[][] sortTable(char[][] unsortedTable) {
  24. //Create new array to copy result to
  25. char[][] sortedTable = new char[unsortedTable.length][unsortedTable[0].length];
  26. //Copy result to List of custom objects
  27. ArrayList&lt;IndexTuple&gt; tupleList = new ArrayList&lt;&gt;();
  28. for (int i = 0; i &lt; unsortedTable[0].length; i++) {
  29. tupleList.add(new IndexTuple(unsortedTable[0][i], i));
  30. }
  31. //sort list
  32. tupleList.sort(new TupleComparator());
  33. //Using original unsorted index and new sorted index set all the values in correct order
  34. for (int i = 0; i &lt; unsortedTable.length; i++) {
  35. for (int j = 0; j &lt; tupleList.size(); j++) {
  36. sortedTable[i][j] = unsortedTable[i][tupleList.get(j).unsortedIndex];
  37. }
  38. }
  39. return sortedTable;
  40. }

huangapple
  • 本文由 发表于 2020年10月16日 06:30:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/64380520.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定