java - 如何使用不同的数据类型进行交换

标签 java arrays numbers swap words

好吧,假设我有一个 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:- 很抱歉给您带来任何不便。提前致谢

最佳答案

您可以执行以下操作..

  1. 您可以创建一个类并存储它,而不是单独存储。

    public class Numbers {
    String word;
    Integer digit;
    
    public Numbers(String w, int d) {
        word = w;
        digit = d;
    }
    

    }

  2. 使用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/

相关文章:

java - DialogFragment 保持全屏显示(不在对话框中)

java - 更新 Android 应用中的原始资源

java - 如何在 Java 的正则表达式中使用 OR ?

python - 在 numpy 数组的末尾追加数组的 n 个副本

PHP,用其他数组变量替换数组变量

c - 线程和并行编程

java - JavaO 怎么样?

python - 函数调用的结果不是正确的 float 数组。解决

Android:如何检查两个单击按钮中的两个数字是否相等(内存游戏)?

numbers - 计算算术 - 8 位数字需要多少位