0

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.

1
  • The exception you're getting is because you're using i1 for subList2, which, I assume, doesn't have i1 elements. Commented Oct 7, 2013 at 14:28

4 Answers 4

1

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]
Sign up to request clarification or add additional context in comments.

1 Comment

Thankyou very much. Just what I was after. Much appreciated.
1

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
                                           _____

1 Comment

Still IOBE issue when li.size()> l2.size()
0

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

1 Comment

In the end I actually used part of this so thank you for the reply. I accepted other answer based on him answering first.
0

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.

1 Comment

Still IOBE issue when li.size()> l2.size()

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.