好吧,假设我有一个 4 个整数的数组 ==
int[] input = new int[]{1,2,3,4};
//now I some how converted these numbers into words and gets an array:-
String Str = new String[]{one,two,three,four};
//then I run a sort method on Str.
Arrays.sort(Str);
//and I got this array in the end ::: {four,one,three,two}
现在我想将其重新转换为数字(int),以便我的最终结果应如下所示:- {4,1,3,2}
夏季:- 我想通过其英语(单词)表示对输入 int 数组进行排序
Examples :-
[input] [convert to words] [sorting] [Result]
{1,2,3,4} => {one,two,three,four} =>{four,one,three,two} => 4,1,3,2
{55,23,3,45} =>{fifty five, twenty three, three, fourty five} => {fifty five, fourty five, three, twenty three} => {55,45,3,23}
所以,到目前为止,我设法将输入转换为单词并对其进行排序,但我不知道如何将 Str 数组
转换回其原始 int 数组,如上面的示例所示......
THE REAL QUESTION :- I know i can convert it using loops and array..but i wanna know is it possible to do it using array swapping??[maybe through its indexes??]
P.s:- 很抱歉给您带来任何不便。提前致谢
最佳答案
您可以执行以下操作..
您可以创建一个类并存储它,而不是单独存储。
public class Numbers { String word; Integer digit; public Numbers(String w, int d) { word = w; digit = d; }
}
使用
Arrays.sort
并创建自定义比较器Arrays.sort(n, new Comparator<Numbers>() { public int compare(Numbers word, Numbers o) { return word.digit.compareTo((o.digit)); } });
关于java - 如何使用不同的数据类型进行交换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43246545/