So if we have a list of strings, for example
ArrayList<String> listA = new ArrayList(Arrays.asList("C", "E", "B", "F", **"E"**, "A", "G", "G", "C", "A", "B", **"G"**));
And there's another list:
ArrayList<String> listB = new ArrayList<>(Arrays.asList("E", "D", "C", "D", **"E"**, "E", "E", "D", "D", "D", "E", **"G"**));
As you can see, these two lists match on positions 4 and 11, E and G are two letters that match that is it's the same letter on the same position.
What I want to do is replace values in listB that don't match values in listA, that is all other letters that are not E on 4 and G on 11 should be new random letters from this list
ArrayList<String> someListOfValues = new ArrayList(Arrays.asList("C", "C#", "D", "Eb", "E", "F", "F#", "G", "G#", "A", "Bb", "B"));
but two that match should remain.
I tried creating a new list that would remember positions where lists didn't match
Like this
ArrayList<Integer> unmatchingPositions = new ArrayList<>();
ArrayList<Integer> matchingPositions = new ArrayList<>();
for (int j = 0; j < listA.size(); j++) {
if (ListA.get(j).equals(ListB.get(j))) {
matchingPositions.add(j);
} else unmatchingPositions.add(j);
}
Then I want to find these values in listB and replace them with random values
for (int j = 0; j < listA.size(); j++) {
for (int k = 0; k < unmatchingPositions.size(); k++) {
if (k == unmatchingPositions.get(k)) {
listB.set(k, someListOfValues.get(rand.nextInt(someListOfValues.size())));
}
}
}
But this doesn't work and I can't figure out what's the problem.
if (k == unmatchingPositions.get(k)) {
This is saying if the value stored in position k of the unmatched list == k then replace it. That makes no sense. You know it's not matched so just replace it. No need for an if
You could do it all in one pass:
int listASz = listA.size();
for(int i = 0; i < listASz; i++)
{
if (listA[i] != listB[i])
{
listB[i] = /*something*/
You need to extend this a bit to handle cases where the lists aren't the same size