Reputation:
I have an issue regarding arrayList element while in looping I have two lists : the first list contains 2 elements : A and B. the second list contains 4 element : A1,B1,B2,B3 Now, what I want is to get from both the list to match their respective elements For example: A -> A1 B -> B1 B -> B2 B -> B3
public class MyArrayList {
public static void main(String[] args) {
List listA = new ArrayList<>();
List listB = new ArrayList<>();
listA.add("ElementA");
listA.add("ElementB");
listB.add("ElementA1");
listB.add("ElementB1");
listB.add("ElementB2");
listB.add("ElementB3");
listB.add("ElementB4");
for (int i = 0; i < listA.size(); i++) {
for (int j = 0; i < listB.size(); j++) {
System.out.println(listA.get(i) + "-->" + listB.get(j));
}
}
}
}
Upvotes: 1
Views: 54
Reputation: 28289
You code got some synax errors. You can print them when element from A is substring of element from B using String.contains()
.
public class MyArrayList {
public static void main(String[] args) {
List listA = new ArrayList<>();
List listB = new ArrayList<>();
listA.add("ElementA");
listA.add("ElementB");
listB.add("ElementA1");
listB.add("ElementB1");
listB.add("ElementB2");
listB.add("ElementB3");
listB.add("ElementB4");
for (Object elementFromFirstList : listA) {
for (Object elementFromSecondList : listB) {
if (((String) elementFromSecondList).contains(
(String) elementFromFirstList)) {
System.out.println(elementFromSecondList + "-->" + elementFromFirstList);
}
}
}
}
}
Upvotes: 1