Suppose, I have a list: firstList = ["Travel", "Shopping", "Transport"];
another list: secondList = ["Travel", "Shopping", "Shopping", "Travel", "Transport", "Transport", "Travel", "Travel"]
I need to find the index of all the elements from the secondList that contains: "Travel"
Here in secondList "Travel" is present in index 0, 3, 6, and 7
Now, I have a 3rd List containing the index of the element, here "Travel".
indexList = [0, 3, 6, 7] // Since index 0, 3, 6, and 7 only contains the element "Travel".
Below is my program:
for (int i = 0; i < firstList.length; i++) {
for (int j = 0; j < secondList.length; j++) {
indexList.add(secondList.indexOf(firstList[i]));
}
}
This is not working as I am getting output like this:
[0, 0, 0, 0, 0, 0, 0, 0, 0]
Seems like it stuck on the first index itself.
The "Travel", here is an example, it should be matching dynamically such, firstList[i] or any other element not just "Travel" as hardcoded. Such as if I have selected firstList[i] then find the index of the same element in the secondList[].
Please help me to identify the cause. I am new to programming.