简体   繁体   English

在两个ArrayLists中排序和交换元素(Java)

[英]Sorting and swapping elements in two ArrayLists (Java)

I am new to Java... 我是Java新手...

I have two ArrayLists: 我有两个ArrayLists:

subList1
subList2

They have been populated already by another method and when run, the lists contain the following Strings: 它们已经由另一种方法填充,并且在运行时,列表包含以下字符串:

subList1: [amulet, map, stone, sword]
subList2: [bottle, shield, wand]

What I need to be able to do is sort both lists so that subList1 contains all elements smaller than the elements in subList2 in terms of alphabetical postion. 我需要做的是对两个列表进行排序,以便在字母顺序方面,subList1包含的所有元素都小于subList2中的元素。 Also both list sizes must stay the same. 同样,两个列表大小必须保持相同。

Expected output: 预期产量:

subList1: [amulet, bottle, map, shield]
subList2: [stone, sword, wand]

My code so far: 到目前为止,我的代码:

Collections.sort(subList1);
Collections.sort(subList2);

    //[amulet, map, stone, sword]
    //[bottle, shield, wand]

    for (int i1 = 0; i1 < subList1.size(); i1++) {

        for (int i2 = 0; i2 < subList2.size(); i2++) {

        if (subList1.get(i1).compareTo(subList2.get(i1)) < 0) {

            // first run: element 0: subList1 = amulet, subList2 = bottle

            String temp = subList1.get(i1);
            subList1.set(i1, subList2.get(i1));
            subList2.set(i1, subList1.get(i1));  

I also get IndexOutOfBoundsException for the following line: 我还获得了针对以下行的IndexOutOfBoundsException:

if (subList1.get(i1).compareTo(subList2.get(i1)) < 0)

Any help much appreciated. 任何帮助,不胜感激。 Thanks. 谢谢。

What you have (incorrect) 您所拥有的(不正确)

if (subList1.get(i1).compareTo(subList2.get(i1)) < 0) 

What it should be (Correct) 应该是什么(正确)

if (subList1.get(i1).compareTo(subList2.get(i2)) < 0) // you wrote i1 instead of i2
                                           _____

You are complicating your task by sorting the two lists separately, and then iterating over them. 您正在通过分别对两个列表进行排序,然后对其进行迭代来使任务复杂化。 I would suggest you to follow this approach: 我建议您遵循这种方法:

  • Merge the two list to create a newList = subList1 + subList2 合并两个列表以创建一个newList = subList1 + subList2
  • Sort the newList 排序newList
  • Get the sublist equal to the length of subList2 from the end of the newList . 获取子列表等于长度subList2从的末端newList
  • Get the sublist equal to the length of subList1 from the beginning of the newList 获取子表相同的长度subList1从年初newList

Working code: 工作代码:

Collection<String> subList1 = Arrays.asList("amulet", "map", "stone", "sword");
Collection<String> subList2 = Arrays.asList("bottle", "shield", "wand");

// Merge two collection in a single list    
List<String> mergedList = new ArrayList<>(subList1);
mergedList.addAll(subList2);

Collections.sort(mergedList);

// Assign subList from mergedList back to original Collection reference
subList1 = mergedList.subList(0, subList1.size());
subList2 = mergedList.subList(subList1.size(), mergedList.size());

System.out.println(subList1);  // [amulet, bottle, map, shield]
System.out.println(subList2);  // [stone, sword, wand]

This will fix your exception: if (subList1.get(i1).compareTo(subList2.get(i2)) < 0) 这将解决您的异常:if(subList1.get(i1).compareTo(subList2.get(i2))<0)

To sort both lists, you need to take the sizes of the first one. 要对两个列表进行排序,您需要采用第一个列表的大小。 (ie int len = subList1.length()), merge them together, sort them, and then split it into 2 based on the 'len' variable you saved first. (即int len = subList1.length()),将它们合并在一起,对其进行排序,然后根据您首先保存的'len'变量将其分为2个。

Something like this, perhaps: 大概是这样的:

int length = subList1.length();
subList1.addAll(subList2); // add both lists together
Collections.sort(subList1); // sort

// split them both up again
subList2 = subList1.subList(length, subList1.length());
subList1 = subList1.subList(0, length);

This can be neatened up in many ways, but should give you a good place to start from. 可以用很多方法解决这个问题,但是应该为您提供一个良好的起点。 The length variable is unnecessary (we know the length of subList 2), but makes for easy to read code. length变量不是必需的(我们知道subList 2的长度),但是使代码易于阅读。

Merge the two lists: 合并两个列表:

List<String> merged = new ArrayList<String>();
merged.addAll(subList1);
merged.addAll(subList2);

Sort the merged list: 排序合并列表:

Collections.sort(merged);

Get the size of subList1: 获取subList1的大小:

int k = subList1.size();

Clear and add into subList1 the entries of the merged list from 0 to k: 清除并将合并列表的条目从0到k添加到subList1中:

subList1.clear();
subList1.addAll(merged.subList(0, k));

Clear and add into subList2 the entries of the merged list from k to n (where n is the size of the merged list): 清除并将合并列表的条目从k到n添加到subList2中(其中n是合并列表的大小):

subList2.clear();
subList2.addAll(merged.subList(k, merged.size()));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM