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

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

问题描述

我学习,现在了解Java,并同时与阵列练我产生了怀疑。我写了下面code为例:

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.

如何洗牌第二数组的内容相匹配的第一个?的排序要求,该解决方案还必须具有灵活性,以便在该计划的范围和规模的变化。请不要发布任何答案要求我改变我的方法与阵列,或提出一个做事的更有效的方式。这是教育和望目我想直的解决方案提供的例子code。在此先感谢!

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()

推荐答案

下面是不使用任何地图 Col​​lection中的code,但是如果你想使用地图然后就变得非常容易。这两个阵列添加到地图和排序。

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天全站免登陆