- class ArrayDemo
- {
- public static void printArray(int[] arr)
- {
- for (int i = 0;i<arr.length ;i++ )
- {
- System.out.print(arr[i]+" ");
-
- }
- }
- public static int getIndex(int[] arr)
- {
- int index = 0;
- for(int i = 1;i<arr.length;i++)
- {
- if(arr[i]>arr[index])
- {
- index = i;
- }
- }
- return index+1;
- }
- public static int getMax(int[] arr)
- {
- int max = arr[0];
- for(int i = 1;i<arr.length;i++)
- {
- if(arr[i]>max)
- {
- max = arr[i];
- }
- }
- return max;
- }
- public static void sortArray(int[] arr)
- {
- for (int j = 0;j<arr.length-1;j++)
- {
- for(int i = 0;i<arr.length-1-1;i++)
- {
- if(arr[i+1]>arr[i])
- {
- int temp = arr[i];
- arr[i] = arr[i+1];
- arr[i+1] = temp;
- }
- }
- }
- printArray(arr);
-
- }
- public static void main(String[] args)
- {
- int[] arr = {34,-19,11,-109,31,-56,45,12,53,-10,22,42,-62};
- System.out.println(getMax(arr));
- System.out.println("-------------------");
- System.out.println(getIndex(arr));
- sortArray(arr);
- }
- }
复制代码 |