英文:
How can I put alphabetically a double dimensional table?
问题
以下是翻译好的代码部分:
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;
}
System.out.print((char) table2[i][j] + " ");
if (j == userKeyWord.length() - 1)
System.out.println();
}
}
注意:这只是你提供的代码的翻译部分,可能仍然需要检查和调试,以确保其正确性和完整性。另外,你提到的 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:
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
and the result must be:
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
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?
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]) {
// table2[u]
= 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();
}
}
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的虚拟值来填充未使用的数组位置。这使得矩阵更加均匀且更易于处理。
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
英文:
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.
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 } };
Get the first row and sort the indices based on that.
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();
Now use those indices to build a new matrix
for (int row = 0; row < mat.length; row++) {
int[] temp = mat[row];
mat[row] = Arrays.stream(indices).map(i -> temp[i])
.toArray();
}
And then print it
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();
}
Prints
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
</details>
# 答案2
**得分**: 1
我不确定为什么你的代码不起作用,但我以稍微不同的方式来处理它。这是我的做法。
```java
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}};
// 保存原始索引和键的对象
private static class IndexTuple {
protected char key;
protected int unsortedIndex;
public IndexTuple(char key, int unsortedIndex) {
this.key = key;
this.unsortedIndex = unsortedIndex;
}
}
// 比较器,用于让集合进行排序
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) {
// 创建新数组以复制结果
char[][] sortedTable = new char[unsortedTable.length][unsortedTable[0].length];
// 将结果复制到自定义对象的列表中
ArrayList<IndexTuple> tupleList = new ArrayList<>();
for (int i = 0; i < unsortedTable[0].length; i++) {
tupleList.add(new IndexTuple(unsortedTable[0][i], i));
}
// 排序列表
tupleList.sort(new TupleComparator());
// 使用原始未排序索引和新排序索引,将所有值设置为正确的顺序
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;
}
英文:
I am not 100% sure why your code isn't working, but I approached it a little bit differently. Here is what I did.
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;
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论