Chris
Chris

Reputation: 3293

Sorting and swapping elements in two ArrayLists (Java)

I am new to Java...

I have two 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. 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:

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

Any help much appreciated. Thanks.

Upvotes: 0

Views: 1312

Answers (4)

Husman
Husman

Reputation: 6909

This will fix your exception: if (subList1.get(i1).compareTo(subList2.get(i2)) < 0)

To sort both lists, you need to take the sizes of the first one. (i.e. int len = subList1.length()), merge them together, sort them, and then split it into 2 based on the 'len' variable you saved first.

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.

Upvotes: 0

Rohit Jain
Rohit Jain

Reputation: 213311

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
  • Sort the newList
  • Get the sublist equal to the length of subList2 from the end of the newList.
  • Get the sublist equal to the length of subList1 from the beginning of the 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]

Upvotes: 1

sp00m
sp00m

Reputation: 48827

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:

int k = subList1.size();

Clear and add into subList1 the entries of the merged list from 0 to k:

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

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

Upvotes: 0

Ankit Rustagi
Ankit Rustagi

Reputation: 5637

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
                                           _____

Upvotes: 1

Related Questions