How to "propagate" the sorting of an `ArrayList` to another one?
I have multiple ArrayList<String>s "linked" into a custom adapter I'm
using to build a list view. Now suppose they are just two in total, to
simplify. I want to sort one of them and then have this new order
reflected into the other one, in order to maintain the list consistent.
This is what I was thinking to do and doesn't work, ending with an
IndexOutOfBoundsException: Invalid index 0, size is 0 at the line signed
with *.
// declarations:
static List<String> filenameEntries = new ArrayList<String>();
static List<String> idEntries = new ArrayList<String>();
/* various operations that fill
the two ArrayList here... */
// sorting:
List<String> oldFilenameEntries = new ArrayList<String>();
List<String> oldIdEntries = new ArrayList<String>();
oldFilenameEntries = filenameEntries;
oldIdEntries = idEntries;
idEntries.clear();
Collections.sort(filenameEntries);
for (int i = 0; i < filenameEntries.size(); i++ ) {
for (int j = 0; j < oldFilenameEntries.size(); j++ ) {
if (oldFilenameEntries.get(j) == filenameEntries.get(i)) {
idEntries.add(oldIdEntries.get(j)); // exception
}
}
}
My idea was to search into the old ArrayList for every element from the
new one, and then use this "old" index to re-polulate the other ArrayList.
Any suggestion? Thanks.
No comments:
Post a Comment