面向对象程序设计05

数组

public class Arr_1 {
    public static void main(String[] args){
        int[] c =new int[10];
        for (int i=0;i<6;i++){
            c[i]=i+1;
        }
        re(c,6);
        for (int i=0;i<6;i++){
            System.out.println(c[i]);
        }
    }
    public static void re(int[] c,int n){
        int len=n;
        int[] b = new int[n];
        for(int i=0;i<n;i++){
            b[i]=c[len - 1];
            len--;
        }
        for(int i=0;i<n;i++){
            c[i]=b[i];
        }
    }
}
public class Arr_2 {
    public static void main(String[] args) {
        int[] oldArr ={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};
        Out(oldArr);
    }
    public static void Out(int[] a){
        int nonZeroCount = 0;
        for (int i = 0; i < a.length; i++) {
            if (a[i] != 0) {
                nonZeroCount++;
            }
        }
        int[] newArr = new int[nonZeroCount];
        int index = 0;
        for (int i = 0; i < a.length; i++) {
            if (a[i] != 0) {
                newArr[index] = a[i];
                System.out.println(newArr[index]);
                index++;
            }
        }
    }
}
public class Arr_3 {
    public static void main(String[] args) {
        int[] A ={1,7,9,11,13,15,17,19};
        int[] B ={2,4,6,8,10};
        int[] result=add(A,B);
        bubbleSort(result);
        for (int i = 0; i < result.length; i++) {
            System.out.println(result[i]);
        }

    }
    public static int[] add(int[] a, int[] b){
        int[] result = new int[a.length+b.length];
        for (int i = 0; i < a.length; i++){
            result[i]=a[i];
        }
        for (int i = 0; i < b.length; i++){
            result[a.length+i]=b[i];
        }
        return result;
    }

    public static void bubbleSort(int[] arr) {
        int n = arr.length;
        for (int i = 0; i < n - 1; i++) {
            for (int j = 0; j < n - i - 1; j++) {
                if (arr[j] > arr[j + 1]) {
                    int temp = arr[j];
                    arr[j] = arr[j + 1];
                    arr[j + 1] = temp;
                }
            }
        }
    }

}

评论

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注