数组作业

1.定义一个长度为10的int数组,统计数组中的最大值、最小值、以及奇数和偶数的个数

/*
定义一个长度为10的int数组,统计数组中的最大值、最小值、以及奇数和偶数的个数
 */
public class Test01 {
    public static void main(String[] args) {

        int[] arr = {54,14321,54,35,32,6,32,0,432,4321};

        // 找最大值
        int max = findMax(arr);
        System.out.println("最大值:" + max);

        // 找最小值
        int min = findMin(arr);
        System.out.println("最小值:" + min);

        //找奇数和偶数的个数
        int count1 = 0;
        int count2 = 0;
        for(int i = 0; i < arr.length; i++){
            if(arr[i] % 2 == 0){
                count1++;
            }else{
                count2++;
            }
        }
        System.out.println("偶数个数:" + count1);
        System.out.println("奇数个数:" + count2);
    }

    private static int findMin(int[] arr) {
        // 认为第一个元素是最小的
        int min = 0;
        // 遍历数组
        for(int i = 1; i < arr.length; i++){
            if(arr[i] < arr[min]){
                min = i;
            }
        }
        return arr[min];
    }

    private static int findMax(int[] arr) {
        // 认为第一个元素是最大的。
        int max = 0;
        // 遍历数组
        for(int i = 1; i < arr.length; i++){
            if(arr[i] > arr[max]){
                max = i;
            }
        }
        return arr[max];
    }
}

 

2.定义一个Student类型的数组,每一个Student对象都有姓名和考试成绩的属性。
请编写程序对Student数组中的学生按照成绩进行排序。
例如:
Student s1 = new Student("zhangsan", 90);
Student s2 = new Student("lisi", 80);
Student s3 = new Student("wangwu", 99);
Student s4 = new Student("zhaoliu", 75);
Student s5 = new Student("lucy", 60);
Student[] stus = {s1, s2, s3, s4, s5};
编写程序按照学生的成绩进行排序。
排序之后对数组进行遍历输出。

/*
定义一个Student类型的数组,每一个Student对象都有姓名和考试成绩的属性。
请编写程序对Student数组中的学生按照成绩进行排序。
    例如:
        Student s1 = new Student("zhangsan", 90);
        Student s2 = new Student("lisi", 80);
        Student s3 = new Student("wangwu", 99);
        Student s4 = new Student("zhaoliu", 75);
        Student s5 = new Student("lucy", 60);
        Student[] stus = {s1, s2, s3, s4, s5};
        编写程序按照学生的成绩进行排序。
        排序之后对数组进行遍历输出。
 */
public class Test02 {
    public static void main(String[] args) {

        Student s1 = new Student("zhangsan", 90);
        Student s2 = new Student("lisi", 80);
        Student s3 = new Student("wangwu", 99);
        Student s4 = new Student("zhaoliu", 75);
        Student s5 = new Student("lucy", 60);

        Student[] arr = {s1, s2, s3, s4, s5};

        // 使用选择排序
        for(int i = 0; i < arr.length - 1; i++){
            int min = i;
            for(int j = i + 1; j < arr.length; j++){
                if(arr[j].compareTo(arr[min]) < 0){
                    min = j;
                }
            }
            if(i != min){
                Student temp;
                temp = arr[i];
                arr[i] = arr[min];
                arr[min] = temp;
            }
        }

        for(Student s : arr){
            System.out.println(s);
        }

    }
}

 

3. 提取一个方法,将指定数组中的数组元素进行反转
例如:{10,23,2,45,6}—>{6,45,2,23,10}

public class Test03 {
    public static void main(String[] args) {
        // 原始数组
        int[] arr = {10,23,2,45,6};

        // 翻转
        reverse(arr);

        System.out.println("=================================");

        // 遍历输出
        for(int a : arr){
            System.out.println(a);
        }

    }

    /**
     * 翻转数组中的每个元素
     * @param arr
     */
    private static void reverse(int[] arr) {
        //{10,23,2,45,6}
        //{6,45,2,23,10}
        // 不允许新建一个新的数组,用交换位置来完成翻转!
        // 交换位置的次数是:arr.length / 2
        for(int i = 0; i < arr.length / 2; i++){
            // 位置的交换
            // 谁和谁交换?
            // arr[0] - arr[4]
            // arr[1] - arr[3]

            // arr[i] - arr[arr.length-1-i] 小算法!
            int temp;
            temp = arr[i];
            arr[i] = arr[arr.length-1-i];
            arr[arr.length-1-i] = temp;
        }
    }
}

 

4. 利用选择排序对数据进行降序排序

/**
 * 利用选择排序对数据进行降序排序
 *
 * 每次找出最大的和最左侧元素交换位置。
 */
public class Test04 {
    public static void main(String[] args) {
        int[] arr = {10,8,6,4,3};
        for(int i = 0; i < arr.length - 1; i++){
            int max = i;
            for(int j = i + 1; j < arr.length; j++){
                if(arr[j] > arr[max]){
                    max = j;
                }
            }
            if(i != max){
                int temp;
                temp = arr[i];
                arr[i] = arr[max];
                arr[max] = temp;
            }
        }

        for(int num : arr){
            System.out.println(num);
        }
    }
}

 

5.找出一个数组中有哪些重复元素, 并且这些元素各重复了几次

import java.util.Arrays;

/**
 * 找出一个数组中有哪些重复元素, 并且这些元素各重复了几次
 */
public class Test05 {
    public static void main(String[] args) {

        int[] arr = {543,543,4,265,4,325,432,5,432,5,432,265};

        // 对以上数组排序
        Arrays.sort(arr);

        //遍历
        /*for(int a : arr) {
            System.out.println(a);
        }*/

        /*
        4
        4
        5
        5
        265
        265
        325
        432
        432
        432
        543
        543
         */

        // 将第1个元素记住
        int index = 0;
        while(index < arr.length){

            // 参与比较的第一个元素
            int firstElement = arr[index];

            // 这个元素至少有1个。
            int count = 1;

            for(int i = index + 1; i < arr.length; i++){
                if(arr[i] == firstElement){
                    count++;
                }else{
                    break;
                }
            }
            if(count != 1){
                System.out.println(firstElement + "重复了:" + count + "次");
            }
            index += count;
        }


    }

}

 

posted @ 2020-05-28 23:12  严重焦虑症  阅读(316)  评论(0编辑  收藏  举报