String數組中的特殊的比較!

題目:
寫一個java API:
 
1. 輸入兩個String數組;
2. 輸出boolean;
3. 判斷兩個String數組內容是否一樣(只比較有效內容:數組元素非NULL且非空稱爲有效);

(1) . String[] s1 = null;
        String[] s2 = new String[] {};
        String[] s3 = new String[] { null, null };
        String[] s4 = new String[] { "" };                      讓它兩兩比較都是返回True.

(2) . 去掉數組元素中的前後空格.讓相等的元素合併成一個元素..

(3) . 然後是讓那數組進行無序的比較.如果有相同的元素.就可以返回True.

具體類:

import java.util.ArrayList;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Set;

public class TestTwo {
 /**
  * Is compare main.
  *
  * @param A
  * @param B
  * @return boolean bool.
  */
 public boolean compareCollection(String[] A, String[] B) {
  boolean bool = false;
  boolean isNull = isEqualNull(A) && isEqualNull(B);
  if (isNull) {
   return true;
  } else {
   if (isEqualNull(A) == false && isEqualNull(B) == false) {
    List lstA = removeSame(removeBlank(A));
    List lstB = removeSame(removeBlank(B));
    if (lstA.size() == lstB.size()) {
     System.out.println("a = " + lstA + " ; b  = " + lstB);
     bool = outOfOrder(lstA, lstB);
    } else {
     System.out.println("two col length not equal");
    }
   } else {
    return false;
   }
  }
  return bool;
 }

 /**
  * Compare the col ...
  *
  * @param str
  * @return boolean isNull
  */
 public boolean isEqualNull(String[] str) {
  boolean isNull = false;
  String[] str2 = new String[str.length];
  int count = 0;
  if (str.length > 1) {
   for (int i = 0; i < str.length; i++) {
    if (str[i] != null && !"".equals(str[i].trim())) {
     str2[count++] = str[i];
    }
   }
   if(count > 0) {
    isNull = false;
   } else {
    isNull = true;
   }
  }
  else if (str == null || str.length == 0) {
   isNull = true;
  } else if (str.length == 1
    && (str[0] == null || "".equals(str[0].trim()))) {
   isNull = true;
  }
  return isNull;
 }

 /**
  * Remove the blank.
  *
  * @return List havaBlank.
  */
 public List removeBlank(String[] str) {
  List haveBlank = new ArrayList();
  for (int i = 0; i < str.length; i++) {
   if (str[i] != null && !"".equals(str[i].trim())) {
    haveBlank.add(str[i].trim());
   }
  }
  return haveBlank;
 }

 /**
  * Remove the same.
  *
  * @return List same
  */
 public List removeSame(List lst) {
  List same = new ArrayList();
  Set set = new HashSet();
  Iterator it = lst.iterator();
  while (it.hasNext()) {
   set.add(it.next());
  }
  Iterator iterator = set.iterator();
  while (iterator.hasNext()) {
   same.add(iterator.next());
  }
  return same;
 }

 /**
  * Out of order.
  *
  * @param lst1
  * @param lst2
  * @return boolean out
  */
 public boolean outOfOrder(List lst1, List lst2) {
  boolean out = true;
  if (lst1.size() == lst2.size()) {
   for (int i = 0; i < lst1.size(); i++) {
    if (!lst2.contains(lst1.get(i))) {
     out = false;
    }
   }
  }
  return out;
 }

 public static void main(String[] args) {

  String[] s1 = null;
  String[] s2 = new String[] {};
  String[] s3 = new String[] { null, null };
  String[] s4 = new String[] { "" };

  String[] s5 = new String[] { null, "A", "", "b", null, "A", "", "d",
    "c", "c", "A", "a", "A" };
  String[] s6 = new String[] { "b", "A", "A", null, "d", "c", "c", "a",
    "A", "a" };

  String[] s7 = new String[] { "a", "b", "a c", " ", "", "", "c " };
  String[] s8 = new String[] { "b", "c", "a", "  a c ", null, " " };
  String[] s9 = new String[] { " " };
  String[] s10 = new String[] { "    " };
  TestTwo t = new TestTwo();
   System.out.println("s1, s2:" + t.compareCollection(s1, s2));
   System.out.println("s1, s3:" + t.compareCollection(s1, s3));
   System.out.println("s1, s4:" + t.compareCollection(s1, s4));
   System.out.println("s1, s5:" + t.compareCollection(s1, s5));
   System.out.println("s1, s6:" + t.compareCollection(s1, s6));
   System.out.println("s2, s3:" + t.compareCollection(s2, s3));
   System.out.println("s2, s4:" + t.compareCollection(s2, s4));
   System.out.println("s2, s5:" + t.compareCollection(s2, s5));
   System.out.println("s2, s6:" + t.compareCollection(s2, s6));
   System.out.println("s3, s4:" + t.compareCollection(s3, s4));
   System.out.println("s3, s5:" + t.compareCollection(s3, s5));
   System.out.println("s3, s6:" + t.compareCollection(s3, s6));
   System.out.println("s4, s5:" + t.compareCollection(s4, s5));
   System.out.println("s4, s6:" + t.compareCollection(s4, s6));
   System.out.println("s5, s6:" + t.compareCollection(s5, s6));
   System.out.println("s7, s8:" + t.compareCollection(s7, s8));

 }

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章