转自:http://godsmell.iteye.com/blog/1975776
判断数组中是否存在重复的元素:
- import org.apache.commons.lang.ArrayUtils;
- public boolean isDupInArray(Object[] array) {
- if (ArrayUtils.isEmpty(array) == true) {
- return false;
- }
- for (Object obj : array) {
- if (ArrayUtils.indexOf(array, obj) != ArrayUtils.lastIndexOf(array, obj)) {
- return true;
- }
- }
- return false;
- }
判断数组中某个元素出现的次数:
- import org.apache.commons.lang.ArrayUtils;
- public int countMatches(Object[] array, Object value) {
- if (ArrayUtils.isEmpty(array) || value == null) {
- return 0;
- }
- int cnt = 0;
- int idx = 0;
- while ((idx = ArrayUtils.indexOf(array, value, idx)) > 0) {
- idx++;
- cnt++;
- }
- return cnt;
- }