题目:
写一个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));
}
}