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.
i1forsubList2, which, I assume, doesn't havei1elements.