ArrayUtils工具类
来源:互联网 发布:2017年新出的网络剧 编辑:程序博客网 时间:2024/04/30 15:02
package cn.trinea.android.common.util;/** * Array Utils * <ul> * <li>{@link #isEmpty(Object[])} is null or its length is 0</li> * <li>{@link #getLast(Object[], Object, Object, boolean)} get last element of the target element, before the first one * that match the target element front to back</li> * <li>{@link #getNext(Object[], Object, Object, boolean)} get next element of the target element, after the first one * that match the target element front to back</li> * <li>{@link #getLast(Object[], Object, boolean)}</li> * <li>{@link #getLast(int[], int, int, boolean)}</li> * <li>{@link #getLast(long[], long, long, boolean)}</li> * <li>{@link #getNext(Object[], Object, boolean)}</li> * <li>{@link #getNext(int[], int, int, boolean)}</li> * <li>{@link #getNext(long[], long, long, boolean)}</li> * </ul> * * @author <a href="http://www.trinea.cn" target="_blank">Trinea</a> 2011-10-24 */public class ArrayUtils { private ArrayUtils() { throw new AssertionError(); } /** * is null or its length is 0 * * @param <V> * @param sourceArray * @return */ public static <V> boolean isEmpty(V[] sourceArray) { return (sourceArray == null || sourceArray.length == 0); } /** * get last element of the target element, before the first one that match the target element front to back * <ul> * <li>if array is empty, return defaultValue</li> * <li>if target element is not exist in array, return defaultValue</li> * <li>if target element exist in array and its index is not 0, return the last element</li> * <li>if target element exist in array and its index is 0, return the last one in array if isCircle is true, else * return defaultValue</li> * </ul> * * @param <V> * @param sourceArray * @param value value of target element * @param defaultValue default return value * @param isCircle whether is circle * @return */ public static <V> V getLast(V[] sourceArray, V value, V defaultValue, boolean isCircle) { if (isEmpty(sourceArray)) { return defaultValue; } int currentPosition = -1; for (int i = 0; i < sourceArray.length; i++) { if (ObjectUtils.isEquals(value, sourceArray[i])) { currentPosition = i; break; } } if (currentPosition == -1) { return defaultValue; } if (currentPosition == 0) { return isCircle ? sourceArray[sourceArray.length - 1] : defaultValue; } return sourceArray[currentPosition - 1]; } /** * get next element of the target element, after the first one that match the target element front to back * <ul> * <li>if array is empty, return defaultValue</li> * <li>if target element is not exist in array, return defaultValue</li> * <li>if target element exist in array and not the last one in array, return the next element</li> * <li>if target element exist in array and the last one in array, return the first one in array if isCircle is * true, else return defaultValue</li> * </ul> * * @param <V> * @param sourceArray * @param value value of target element * @param defaultValue default return value * @param isCircle whether is circle * @return */ public static <V> V getNext(V[] sourceArray, V value, V defaultValue, boolean isCircle) { if (isEmpty(sourceArray)) { return defaultValue; } int currentPosition = -1; for (int i = 0; i < sourceArray.length; i++) { if (ObjectUtils.isEquals(value, sourceArray[i])) { currentPosition = i; break; } } if (currentPosition == -1) { return defaultValue; } if (currentPosition == sourceArray.length - 1) { return isCircle ? sourceArray[0] : defaultValue; } return sourceArray[currentPosition + 1]; } /** * @see {@link ArrayUtils#getLast(Object[], Object, Object, boolean)} defaultValue is null */ public static <V> V getLast(V[] sourceArray, V value, boolean isCircle) { return getLast(sourceArray, value, null, isCircle); } /** * @see {@link ArrayUtils#getNext(Object[], Object, Object, boolean)} defaultValue is null */ public static <V> V getNext(V[] sourceArray, V value, boolean isCircle) { return getNext(sourceArray, value, null, isCircle); } /** * @see {@link ArrayUtils#getLast(Object[], Object, Object, boolean)} Object is Long */ public static long getLast(long[] sourceArray, long value, long defaultValue, boolean isCircle) { if (sourceArray.length == 0) { throw new IllegalArgumentException("The length of source array must be greater than 0."); } Long[] array = ObjectUtils.transformLongArray(sourceArray); return getLast(array, value, defaultValue, isCircle); } /** * @see {@link ArrayUtils#getNext(Object[], Object, Object, boolean)} Object is Long */ public static long getNext(long[] sourceArray, long value, long defaultValue, boolean isCircle) { if (sourceArray.length == 0) { throw new IllegalArgumentException("The length of source array must be greater than 0."); } Long[] array = ObjectUtils.transformLongArray(sourceArray); return getNext(array, value, defaultValue, isCircle); } /** * @see {@link ArrayUtils#getLast(Object[], Object, Object, boolean)} Object is Integer */ public static int getLast(int[] sourceArray, int value, int defaultValue, boolean isCircle) { if (sourceArray.length == 0) { throw new IllegalArgumentException("The length of source array must be greater than 0."); } Integer[] array = ObjectUtils.transformIntArray(sourceArray); return getLast(array, value, defaultValue, isCircle); } /** * @see {@link ArrayUtils#getNext(Object[], Object, Object, boolean)} Object is Integer */ public static int getNext(int[] sourceArray, int value, int defaultValue, boolean isCircle) { if (sourceArray.length == 0) { throw new IllegalArgumentException("The length of source array must be greater than 0."); } Integer[] array = ObjectUtils.transformIntArray(sourceArray); return getNext(array, value, defaultValue, isCircle); }}
0 0
- ArrayUtils工具类
- NumberUtils、ArrayUtils和RandomUtils工具类用法
- NumberUtils、ArrayUtils和RandomUtils工具类用法
- NumberUtils、ArrayUtils和RandomUtils工具类用法
- NumberUtils、ArrayUtils和RandomUtils工具类用法
- NumberUtils、ArrayUtils和RandomUtils工具类用法
- Arrayutils数组工具类的学习
- NumberUtils、ArrayUtils和RandomUtils工具类用法
- NumberUtils、ArrayUtils和RandomUtils工具类用法
- NumberUtils、ArrayUtils和RandomUtils工具类用法
- Apache下的ArrayUtils工具类总结
- 数组工具类 org.apache.commons.lang.ArrayUtils
- ArrayUtils
- ArrayUtils
- ArrayUtils
- ArrayUtils
- Java高级个人笔记(NumberUtils、ArrayUtils和RandomUtils工具类用法)
- StringUtils类和ArrayUtils类使用
- Lua基本语法介绍
- 如何将decimal转换为dbnull
- 冒泡排序代码详解
- 第4周 项目3-单链表应用(3)
- 第6周项目5-后缀表达式
- ArrayUtils工具类
- 浅谈 PHP 与手机 APP 开发(API 接口开发)
- 第4周实践项目1-- 建立单链表
- grape api单元测试
- 第七周项目二~~链队算法库
- [原]iOS--字符编码,NSString和UTF-8间的转换
- Impala005-Impala SQL操作内部表和外部表
- xcode 7 错误:CGContextSaveGState: invalid context 0x0. If you want to see the backtrace, please set CG
- 第8周 项目2—建立链串算法库