Syncronized 2的ArrayList之间整理 [英] Syncronized sorting between two ArrayLists

查看:135
本文介绍了Syncronized 2的ArrayList之间整理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个的ArrayList

I have two ArrayLists.


  • 第一个包含一组词用大写和
    标点符号。

  • The first contains a group of words with capitalization and punctuation.

另外包含此相同的基团的话,但与
大小写和标点符号去掉。

The other contains this same group of words, but with the capitalization and punctuation removed.

ArrayList1 ..... ArrayList2

MURDER! ........ murder

It's ........... its

Hello .......... hello

Yes-Man ........ yesman

ON ............. on

第二个数组有按字母顺序排列的所有单词和字母顺序排列的每个字的所有字母。它看起来是这样的:

The second array has all the words alphabetized and all the letters in each word alphabetized. It looks something like this:

aemnsy
demrru
ehllo
ist
no

我想使它这样,当我安排在ArrayList中的两个单词按字母顺序,全部从ArrayList的一次后续套件的话:

I want to make it so that when I arrange the words in ArrayList two into alphabetical order, all the words from ArrayList one follow suite:

ArrayList1 ..... ArrayList2

Yes-Man ........ aemnsy

MURDER! ........ demrru

Hello .......... ehllo

It's ........... ist

ON ............. no

我试图使用的两个语句循环,但它结束了没有工作,变得很长。我该怎么做呢?我该怎么做这有效?

I tried to make a loop with a for statement or two, but it ended up not working and became very long. How do I do this? How do I do this efficiently?

推荐答案

第一种方式 - 我使用的地图,其关键是要从arrayList2和值是arrayList1。把数据地图给你。排序arrayList2后,我从地图上它的价值。

  List<String> arrList1 = new ArrayList<String>();

            arrList1.add("MURDER!");
            arrList1.add("It's");
            arrList1.add("Hello");
            arrList1.add("Yes-Man");
            arrList1.add("ON");

            List<String> arrList2 = new ArrayList<String>();
            arrList2.add("demrru");
            arrList2.add("aemnsy");
            arrList2.add("ist");
            arrList2.add("ehllo");
            arrList2.add("no"); 

            Map<String, String> map1 = new HashMap<String, String>();
            map1.put("aemnsy", "Yes-Man");
            map1.put("demrru", "MURDER!");
            map1.put("ehllo", "Hello");
            map1.put("ist", "It's");
            map1.put("no", "ON");

            Collections.sort(arrList2);

            for (String s : arrList2){
                System.out.println(s + "..........." + map1.get(s));
            }

第二种方式 - 另一种方法是,你只能使用其中已经排序,而不是两个ArrayList中的TreeMap

Map<String, String> map2 = new TreeMap<String, String>();
            map2.put("ehllo", "Hello");
            map2.put("aemnsy", "Yes-Man");
            map2.put("demrru", "MURDER!");
            map2.put("no", "ON");
            map2.put("ist", "It's");

            for (Map.Entry<String, String> entry : map2.entrySet())
            {
                System.out.println(entry.getKey() + "/" + entry.getValue());
            }

第三条道路 - 仅用2 ArrayList中,但是我们必须写排序由我们自己的方法。你有没有注意到你的ArrayList 2等元素从arrayList1 arrayList2和应声虫aemnsy具有相同的索引?我用这一点。

  selectionSort1(arrList2, arrList1);

    for(int i = 0; i < arrList1.size(); i++){
        System.out.println(arrList2.get(i) + "---" + arrList1.get(i));
    }

   public static void selectionSort1(List<String> x, List<String> y) {
    for (int i=0; i<x.size()-1; i++) {
        for (int j=i+1; j<x.size(); j++) {
            if (x.get(i).compareTo(x.get(j)) > 0) {
                //... Exchange elements in first array
                String temp = x.get(i);
                x.set(i, x.get(j));
                x.set(j, temp);

                //... Exchange elements in second array
                temp = y.get(i);
                y.set(i, y.get(j));
                y.set(j, temp);
            }
        }
    }
}

这篇关于Syncronized 2的ArrayList之间整理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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