/**
* 查找指定的元素是否存在于元素数组中
* 使用Object支持各种对象数组
* 示例
* String[] des = {“ada”, “ss”, “dd”,”aa”};
* System.out.println(BaseFunction.contains(str, “adfab”));
* @param objArray Object[]
* @param value Object
* @return int 如果查到则返回第一次在数组中出现的下标值,查不到则返回值为-1
*/
public static int contains(Object[] objArray, Object value) {
int j = -1;
for (int i = 0; i < objArray.length; i++) {
if (objArray.equals(value)) { //查到则返回下标
j = i;
break;
}
}
return j;
}
/**
* 字符串数组中是否包含指定的字符串。
* @param strings 字符串数组
* @param string 字符串
* @param caseSensitive 是否大小写敏感(true区分大小写,false不区分大小写)
* @return 包含时返回true,否则返回false
* @since 0.4
*/
public static boolean contains(String[] strings, String string,
boolean caseSensitive) {
for (int i = 0; i < strings.length; i++) {
if (caseSensitive == true) {
if (strings.equals(string)) {
return true;
}
} else {
if (strings.equalsIgnoreCase(string)) {
return true;
}
}
}
return false;
}
/**
* 数组拷贝,建议使用System.arraycopy()速度更快,把source数据内容拷贝到destination中
* 使用Object则支持多种对象数组
* 示例
* String[] des = {"ada", "ss", "dd","aa"};
* String[] src = new String[8];
* BaseFunction.copy(des, src);
* @param source Object[]
* @param destination Object[]
*/
public static void copy(Object[] source, Object[] destination) {
if (destination == null || source == null) {
throw new java.lang.nullPointerException("数组没有初始化!");
}
if (destination.length < source.length) {
throw new java.lang.ArrayIndexOutOfBoundsException("两数组长度不相等!");
}
for (int i = 0; i < source.length; i++) {
destination = source;
}
}
/**
* 去除数组中的重复元素,调用示例
* String[] src = {"dafdfad", "asdfasdf", "dafdfad", "adfasdf", "dafdfad","dafdfad"};
* Object[] test =BaseFunction.wipeOffRepeat(src);
* @param objArray Object[]
* @return Object[]
*/
public static Object[] wipeOffRepeat(Object[] objArray) {
Object[] obj = null;
ArrayList list = new ArrayList();
for (int i = 0; i < objArray.length; i++) {
list.add(objArray);
}
//去除list中的重复对象
HashSet h = new HashSet(list);
list.clear();
list.addAll(h);
obj = list.toArray();
return obj;
}
/**
* 拼合一维数组为字符串。
* 拆分字串按指定字符分解到一维数组使用String类的split(String regex)
* @param param String[] 数组
* @param spilt String 字符串之单的分离符
* @return String
*/
public static String arrayToString(String[] param, String spilt) {
String rentunstring;
StringBuffer tempstr = new StringBuffer();
int len = param.length;
for (int i = 0; i < len - 1; i++) {
tempstr.append(param);
tempstr.append(spilt);
}
tempstr.append(param[len - 1]);
rentunstring = tempstr.toString();
return rentunstring;
}
/**
* 产生在start和end之间的num个随机整数,返回值存在HashMap中。
* 示例
* HashMap hm=BaseFunction.random(1,100,5);
* Set set=hm.keySet();
* Iterator it=set.iterator();
* while(it.hasNext()){
* System.out.println(hm.get(it.next()));
* }
* @param start int 起始点
* @param end int 结束点
* @param num int 生成个数
* @return HashMap
*/
public static HashMap random(int start, int end, int