日期:2014-05-20 浏览次数:20775 次
public class NameSystem1 { public List BMoreToA(String[] a, String[] b) { List temp = new ArrayList(); for (int i = 0; i < b.length; i++) { boolean flag = false; for (int j = 0; j < a.length; j++) { if (b[i].equals(a[j])) { flag = true; break; } else { flag = false; } } if (!flag) { temp.add(b[i]); } } return temp; } public static void main(String[] args) { NameSystem1 test = new NameSystem1(); String[] a = { "x", "y", "xy", "yx" }; String[] b = { "xy", "y", "yx", "a", "b" }; List temp = null; temp = test.BMoreToA(a, b); System.out.println("B比A多的字符串"); for (int i = 0; i < temp.size(); i++) { System.out.print(temp.get(i) + " "); } System.out.println(""); System.out.println("B比A少的字符串"); temp = test.BMoreToA(b, a); for (int i = 0; i < temp.size(); i++) { System.out.print(temp.get(i) + " "); } } }
------解决方案--------------------
import java.util.List; import java.util.ArrayList; import java.util.Iterator; /** * Author: db2admin * Date : 2010-2-4 * Time : 15:41:37 * Comment: */ public class test { public static List toArrayList(String[] temp) { List templist=new ArrayList(); for (int i = 0; i < temp.length; i++) { templist.add(temp[i]); } return templist; } public static List romove(List lista,List listb){ lista.removeAll(listb); return lista; //System.out.println(lista); } public static List compareArr(String[] a,String[] b){ List commonlist=new ArrayList(); if(a.length<b.length){ for(int i=0;i<a.length;i++){ if(a[i].equals(b[i])) commonlist.add(a[i]); } } return commonlist; } public static void main(String[] args){ String[] a = {"x", "y", "xy", "yx"}; String[] b = {"xy", "y", "yx", "a", "b"}; // A-B System.out.println(test.romove(test.toArrayList(a),test.toArrayList(b))); // B-A System.out.println(test.romove(test.toArrayList(b),test.toArrayList(a))); // A-(A-B) 公共集 System.out.println( test.romove(test.toArrayList(a),test.romove(test.toArrayList(a),test.toArrayList(b))) ); // 公共集-顺序值相同集 System.out.println( test.romove( test.romove(test.toArrayList(a),test.romove(test.toArrayList(a),test.toArrayList(b))), test.compareArr(a,b)) ); } }