java数组工具类

/**
建立一个用于操作数组的工具类,其中包含着常见的对数组的操作的函数:如:最值 排序等
@author 东哥
@version V1.0 
 */
public class ArrayTool {

private ArrayTool(){}//默认的构造函数,该类中的方法都是静态的,所以该类是不需要创建对象的
//为了保证不让其他类创建对象,可以将构造函数私有化
    
/**
 * 获取整型数组的最大值
 * @param arr 接收一个元素为int类型的数组
 * @return 返回该数组中的最大元素值
 * */
public  static int getMax(int[] arr){
    int maxIndex = 0;
    for(int x = 1; x< arr.length;x++){
        if(arr[x] > arr[maxIndex]){
            maxIndex = x;
        } 
    }
    return arr[maxIndex];
}

/**
 * 对数组进行排序
 * @author 
 * @param 接收一个int类型的数组
 * */

public static void selectSort(int[] arr){
    for(int x =0;xarr[y])
                swap(arr,x,y);
        }
    }
}

private static void swap(int[] arr,int a,int b){
    int temp = arr[a];
    arr[a] = arr[b];
    arr[b] = temp;
}


public static int getIndex(int[] arr,int key){
    for(int x = 0;x

你可能感兴趣的:(java数组工具类)