我接受了一次面试,这个问题是他们在技术面试中问我的问题。我的答案是以下几行代码:
public static void main(String[] args) {
String[] temp1 = {"a", "b", "c"};
String[] temp2 = {"c", "d", "a", "e", "f"};
String[] temp3 = {"b", "c", "a", "a", "f"};
ArrayList<String> list1 = new ArrayList<String>(Arrays.asList(temp1));
System.out.println("list1: " + list1);
ArrayList<String> list2 = new ArrayList<String>(Arrays.asList(temp2));
System.out.println("list2: " + list2);
ArrayList<String> list3 = new ArrayList<String>(Arrays.asList(temp3));
System.out.println("list3: " + list3);
list1.retainAll(list2);
list1.retainAll(list3);
for (String str : list1)
System.out.println("Commons: " + str);
}
输出:
list1: [a, b, c]
list2: [c, d, a, e, f]
list3: [b, c, a, a, f]
Commons: a
Commons: c