比较两个字符数组,比较难。
现在比较两个字符数组:String []a={"x","y","xy","yx"},String[]b={"xy","y","yx","a","b"}
最后需要记录反应三种信息:
1、b比a多哪些字符串?
2、b比a少哪些字符串?
3、b与a中顺序不同的字符串有哪些?
[解决办法]
前两个我帮你做出来了
最后一个不知道你要的结果是怎么样所以就不帮你做了,
如果不知道,写清楚需要什么我再帮你写
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)) ); }}