您的位置:首页 > 编程语言 > Java开发

数组操作大全<Java实现>

2012-11-27 19:49 661 查看
/**
* 数组操作类
* @author Sking
*/

import java.util.LinkedHashSet;
import java.util.Set;

public class ArrayUtils {

/**1.复制数组*/
//System.arraycopy(from,fromIndex,to,toIndex,count);

//Xxx代表long,int,short,char,byte,double,float,object,返回Xxx[]数组
//Arrays.copyof(Xxx[] a,int len)
//Arrays.copyOfRange(Xxx[] a,int from,int to)

/**2.数组升序快速排序*/
//Xxx代表long,int,short,char,byte,double,float,Object(实现Comparable接口),返回类型为void
//Arrays.sort(Xxx a[]);
//Arrays.sort(Xxx a[],int fromIndex,int toIndex);

//自定义函数,将数组从中间位置向两边排序展开
/**
* 将指定数组从中间位置向两边排序展开,先左后右
* @param input 待排序数组
*/
@SuppressWarnings({ "rawtypes", "unchecked" })
public static void sort(Comparable[] input){
int n=input.length;
Comparable[] output=new Comparable
;
int middle=n/2;
Comparable[] temp=input;
for(int i=n;i>1;i--){
int maxPos=0;
for(int j=0;j<i;j++)
if(temp[j].compareTo(temp[maxPos])>0)
maxPos=j;
swap(temp,maxPos,i-1);
}//将temp数组降序排序
output[middle]=temp[0];//存放最大的元素
int left=n/2-1;//左边指针
int right=n/2+1;//右边指针
for(int k=1;k<n;k+=2)
output[left--]=temp[k];
for(int m=2;m<n;m+=2)
output[right++]=temp[m];
System.arraycopy(output, 0, input, 0, n);
}

private static void swap(Object[] a,int i,int j){
Object temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}

/**3.数组二分查找*/
//Xxx表示long,int,short,char,byte,double,float,object(实现了Comparable接口),返回索引位置
//Arrays.binarySearch(Xxx a[],Xxx v);
//Arrays.binarySearch(Xxx a[],int from,int to,Xxx v);

/**4.填充数组*/
//Xxx表示long,int,short,char,byte,double,float,Object,返回类型为void
//Arrays.fill(Xxx a[],Xxx v)
//Arrays.fill(Xxx a[],int from,int to,Xxx v)

/**5.比较数组是否相等
* @param <T>*/
//Xxx表示long,int,short,char,byte,boolean,double,float,Object(实现了Comparable接口)
//Arrays.equals(Xxx[] a,Xxx[] b)

/**
* 6.判断数组元素是否重复,T表示类型参数,
*       如果是内置类型使用对应的包装类。
* @param a 输入数组
* @return 如果数组不包含指定重复元素则返回true,否则false
*/
public static <T> boolean ArrayUnique(T[] a){
int len=a.length;
Set<T> set=new LinkedHashSet<T>();
for(T t:a)
set.add(t);
if(len==set.size())
return true;
return false;
}

/**7.打印数组*/
//Xxx代表long,int,short,char,byte,boolean,double,float,Object
//打印格式为:[元素1,元素2,元素3...]
//Arrays.toString(Xxx[] a)

//字符数组转换为字符串
//String.copyValueOf(char[] data[,int offset,int count])
//String.valueOf(char[] data[, int offset, int count])

//自定义函数,使用指定的分割标志符链接数组为字符串
/**
* 使用指定的分割标志链接数组为字符串,
*       如果未指定分隔符则默认使用逗号。
* @param a 待链接数组
* @param separator 分割标志符,没有设置则使用逗号
* @return  使用指定的分割标志链接数组得到的字符串
*/
public static <T> String ArrayToString(T[] a,String separator){
separator=(separator==null?",":separator);
StringBuffer sb=new StringBuffer();
for(T t:a)
sb.append(t.toString()).append(separator);
sb.delete(sb.lastIndexOf(separator), sb.length());
return sb.toString();
}

/**8.反转数组*/
//自定义反转数组函数
/**
* 反转指定数组
* @param a 待反转数组
*/
public static <T> void ArrayReverse(T[] a){
int len=a.length;
for(int i=0,j=len-1;i<j;i++,j--){
T temp=a[i];
a[i]=a[j];
a[j]=temp;
}
}

/**9.求数组最值*/
//见编程问题--数组问题--数组最值.java

/**10.一维数组最大字段和问题*/
//见编程问题--数组问题--一维最大字段和.java

/**11.指定索引插入元素*/
//自定义函数:实现指定索引插入元素到数组中
/**
* 指定索引插入元素到数组中
* @param a 指定数组
* @param value 待插入元素
* @param index 新元素插入位置索引
* @return 插入后的新数组
* @throws Exception 索引不合法
*/
public static Object[] ArrayInsert(Object[] a,Object value,int index) throws Exception{
if(index<0||index>a.length)
throw new Exception("索引不合法!");
Object[] result=new Object[a.length+1];
System.arraycopy(a, 0, result, 0, index);
result[index]=value;
System.arraycopy(a, index, result, index+1,a.length-index);
return result;
}

/**12.指定索引删除元素*/
//自定义函数:实现指定索引删除元素并返回删除后的数组
/**
* 指定索引删除元素并返回删除后的数组
* @param a 指定数组
* @param index 待删除元素的索引
* @return 删除后的数组
* @throws Exception 索引不合法
*/
public static Object[] ArrayDelete(Object[] a,int index)throws Exception{
if(index<0||index>=a.length)
throw new Exception("索引不合法!");
Object[] result=new Object[a.length-1];
System.arraycopy(a, 0, result, 0, index);
System.arraycopy(a, index+1, result, index, a.length-index-1);
return result;
}

public static void main(String[] args){
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: