同时排序两个数组 [英] Sorting two arrays simultaneously

查看:30
本文介绍了同时排序两个数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在正在学习和理解 Java,在练习数组时我有一个疑问.我写了下面的代码作为例子:

I'm learning and understanding Java now, and while practising with arrays I had a doubt. I wrote the following code as an example:

class example
{
    public static void main(String args[])
    {
        String a[] = new String[] {"Sam", "Claudia", "Josh", "Toby", "Donna"};
        int b[] = new int[] {1, 2, 3, 4, 5};

        for(int n=0;n<5;n++)
        {
            System.out.print (a[n] + "...");
            System.out.println (b[n]);
        }
        System.out.println (" ");

        java.util.Arrays.sort(a);

        for(int n=0;n<5;n++)
        {
            System.out.print (a[n] + "...");
            System.out.println (b[n]);
        }
    }

简而言之,这个类创建了两个数组,每个数组有五个空格.它用西翼人物的名字填充一个,并用从一到五的编号填充另一个.可以说这两个字符串中的数据是一一对应的.

In a nutshell, this class created two arrays with five spaces each. It fills one with names of characters from the West Wing, and fills the other with numbering from one to five. We can say that the data in these two strings corresponds to each other.

现在,程序使用 Arrays.sort() 对包含名称的数组进行排序.再次打印数组后,您可以看到虽然名称现在按字母顺序排列,但由于第二个数组未更改,因此数字不再对应.

Now, the program sorts the array with the names in it using Arrays.sort(). After printing the array again, you can see that while the names are now in alphabetical order, the numbers do not correspond anymore as the second array is unchanged.

如何打乱第二个数组的内容以匹配第一个数组的排序要求?该解决方案还必须灵活,以允许更改程序的范围和大小.请不要发布任何要求我改变数组方法的答案,或者提出一种更有效"的做事方式.这是出于教育目的,我想要一个对所提供示例代码的直接解决方案.提前致谢!

How can I shuffle the contents of the second array to match the sort requirements of the first? The solution must also be flexible to allow for changes in the scope and size of the program. Please do not post any answers asking me to change my methodology with the arrays, or propose a more 'efficient' way of doing things. This is for educational purposed and I'd like a straight solution to the example code provided. Thanks in advance!

我不想创建额外的类,但是我认为通过嵌套循环进行排序的某种形式可能是一个选项,而不是 Arrays.sort().

推荐答案

下面是没有使用任何Map集合的代码,但是如果你想使用Map那么它变得很容易.将这两个数组添加到 map 中并对其进行排序.

Below is the code without using any Map Collection, but if you want to use Map then it becomes very easy. Add both the arrays into map and sort it.

public static void main(String args[]) {
    String a[] = new String[] {
        "Sam", "Claudia", "Josh", "Toby", "Donna"
    };
    int b[] = new int[] {
        1, 2, 3, 4, 5
    };
    for (int n = 0; n < 5; n++) {
        System.out.print(a[n] + "...");
        System.out.println(b[n]);
    }
    System.out.println(" ");
    //java.util.Arrays.sort(a);
    /* Bubble Sort */
    for (int n = 0; n < 5; n++) {
        for (int m = 0; m < 4 - n; m++) {
            if ((a[m].compareTo(a[m + 1])) > 0) {
                String swapString = a[m];
                a[m] = a[m + 1];
                a[m + 1] = swapString;
                int swapInt = b[m];
                b[m] = b[m + 1];
                b[m + 1] = swapInt;
            }
        }
    }
    for (int n = 0; n < 5; n++) {
        System.out.print(a[n] + "...");
        System.out.println(b[n]);
    }
}

这篇关于同时排序两个数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆