- class Test3
- {
- public static void main(String[] args)
- {
- String[] a1={"aa","bb","cc","dd","ee"};
- String[] a2={"aa","ff","gg","cc"};
- System.out.println(getStr(a1,a2));//输出aa,cc
- }
- public static String getBuf(ArrayList<String> a1,ArrayList<String> a2)//都是一个想法,就是先把其中一个转换成一个字符串,
- //如果用方法查找,实现最小比较次数得到相同元素
- {
- String s="";
- String str="";
- for(Iterator<String> in=a1.iterator();in.hasNext();)
- s+=in.next();
- for(String s1:a2)
- {
- if(s.contains(s1))
- str+=s1+" ";
- }
- return str;
- }
- public static String getStr(String[] a1,String[] a2)//字符串
- {
- String s=Arrays.toString(a1);
- String s2="";
- for(String s1:a2)
- {
- if(s.contains(s1))//这个只需要判断字符数组的长度,就可以得到结果
- s2+=s1+" ";
- }
- return s2;
- }
- }
复制代码 这算么? |