- class ArrayToolDemo
- {
- public static void main(String[] args)
- {
- int[] arr={1,24,54,46,57,68,78};
- int x=ArrayTool.GetMax(arr);
- System.out.println("Max="+x);
- }
- }
- public class ArrayTool
- {
- private ArrayTool()
- {
- }
- //获取最值
- 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];
- }
- //选择排序
- public static void ArraySelect(int [] arr)
- {
- for (int x=0;x<arr.length-1; x++)
- {
- for (int y=x+1;x<arr.length ; y++)
- {
- if (arr[x]>arr[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 String ArrayToString(int [] arr)
- {
- String str="[";
- for (int x=0;x<arr.length ;x++ )
- {
- if (x!=arr.length-1)
-
- str=str+arr[x]+',';
- else
- str=str+arr[x]+"]";
-
- }
- return str;
- }
- //获取元素角标
- public static int GetIndex(int[] arr,int key)
- {
- for (int x=0;x<arr.length ;x++ )
- {
- if(arr[x]==key)
- return x;
- }
- return -1;
- }
- }
复制代码
|
|