package com.meritit.dm.csdn.sort;
/**
* One util to swap tow element of Array
*
* @author ysjian
* @version 1.0
* @email ysjian_pingcx@126.com
* @QQ 646633781
* @telephone 18192235667
* @csdnBlog http://blog.csdn.net/ysjian_pingcx
* @createTime 2013-12-20
* @copyRight Merit
*/
public class Swaper {
private Swaper() {
}
/**
* Swap tow elements of the array
*
* @param oneIndex
* one index
* @param anotherIndex
* another index
* @param array
* the array to be swapped
* @exception NullPointerException
* if the array is null
*/
public static <T extends Comparable<T>> void swap(int oneIndex,
int anotherIndex, T[] array) {
if (array == null) {
throw new NullPointerException("null value input");
}
checkIndexs(oneIndex, anotherIndex, array.length);
T temp = array[oneIndex];
array[oneIndex] = array[anotherIndex];
array[anotherIndex] = temp;
}
/**
* Swap tow elements of the array
*
* @param oneIndex
* one index
* @param anotherIndex
* another index
* @param array
* the array to be swapped
* @exception NullPointerException
* if the array is null
*/
public static void swap(int oneIndex, int anotherIndex, int[] array) {
if (array == null) {
throw new NullPointerException("null value input");
}
checkIndexs(oneIndex, anotherIndex, array.length);
int temp = array[oneIndex];
array[oneIndex] = array[anotherIndex];
array[anotherIndex] = temp;
}
/**
* Check the index whether it is in the arrange
*
* @param oneIndex
* one index
* @param anotherIndex
* another index
* @param arrayLength
* the length of the Array
* @exception IllegalArgumentException
* if the index is out of the range
*/
private static void checkIndexs(int oneIndex, int anotherIndex,
int arrayLength) {
if (oneIndex < 0 || anotherIndex < 0 || oneIndex >= arrayLength
|| anotherIndex >= arrayLength) {
throw new IllegalArgumentException(
"illegalArguments for tow indexs [" + oneIndex + ","
+ oneIndex + "]");
}
}
}
袁慎建@ThoughtWorks
- 粉丝: 176
- 资源: 66
最新资源
- 组织架构设计流程-企业管理必备.docx
- 【岗位职责说明书】100000301 总经理(院长).doc
- 【岗位职责说明书】100000400 副总经理、院长.doc
- 【岗位职责说明书】100000500 总经理助理、院长助理.doc
- 【岗位职责说明书】100000503 安全保卫部主任.doc
- 【岗位职责说明书】100000601 安全保卫部副主任.doc
- 【岗位职责说明书】100000602 人力资源部副经理.doc
- 【岗位职责说明书】100000604 财务部副经理.doc
- 【岗位职责说明书】100000506 财务部经理.doc
- 【岗位职责说明书】100000702 人力资源部经理助理.doc
- 【岗位职责说明书】100000703 财务部经理助理.doc
- 【岗位职责说明书】100000704 室(职能部室)经理.doc
- 【岗位职责说明书】100000707 综合行政高级主管.doc
- 【岗位职责说明书】100000706 财务部经理(分公司).doc
- 【岗位职责说明书】100000701 安全保卫部主任助理.doc
- 【岗位职责说明书】100000705 综合部经理(分公司).doc
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈