JavaNote 1.2数组

一、数组

1、数组的初始化

动态初始化

Date date[];
date = new Date[3];

静态初始化

Date date[] ={1,2,3};
Date date[]=new Date(){1,2};

2、数组的遍历

int[] array = {2,1,3,4,5,6};
for(int i = 0; i<array.length; i++){
    System.out.println(array[i]);
}

3、数组的拷贝

int[] array ={2,3,4,5,6};
int[] arrayCopy = new int[6];
System.arraycopy(array,0, arrayCopy, 0, array.length);
for(int i=0;i<arrayCopy.length; i++){
    System.out.print(arrayCopy[i]);
}

4、数组的排序

public class Test {
    public static void main(String []args){
        int array[] = {2,3,0,5,1,9,4,8,-1,7};
        Test test = new Test();
        test.sortTest(array);
        System.out.println();
        test.sortTest0(array);
        System.out.println(1/2);
        test.sortTest2(array);
    }
    //冒泡排序
    public void sortTest0(int array[]){
        int temp;
        for(int i = 0; i < array.length;i++){
            for(int j = 0; j < array.length-1; j++){
                if(array[j]>array[j+1]){
                        temp = array[j];
                        array[j] = array[j+1];
                        array[j+1] = temp;
                }
            }

        }
        for (int i = 0; i < array.length; i++){
            System.out.print(array[i]+" ");
        }

    }
    //选择排序
    public void sortTest(int array[]){
        int temp;
        for (int i = 0; i< array.length; i++){
            for(int j =(i + 1); j < array.length; j++){
                if(array[i]>array[j]){
                    temp = array[i];
                    array[i] = array[j];
                    array[j] = temp;
                }
                else
                    continue;
            }

        }
        for (int i = 0; i < array.length; i++){
            System.out.print(array[i]+" ");
        }

    }
    //快速排序
    public  void sortTest2(int array[]){
        for (int i = 0; i < array.length; i++) {
            int temp = array[i];
            int left = 0;
            int right = i-1;
            int mid = 0;
            while(left<=right){
                mid = (left+right)/2;
                if(temp<array[mid]){
                    right = mid-1;
                }else{
                    left = mid+1;
                }
            }
            for (int j = i-1; j >= left; j--) {
                array[j+1] = array[j];
            }
            if(left != i){
                array[left] = temp;
            }
        }
        for (int i = 0; i < array.length; i++) {
            System.out.print(array[i]+" ");
        }
        }
}
例2
public void sort(int a[]){
    int start = 0;
    int temp;
    while (start<a.length-1) {
        for (int i = a.length-1; i >start ; i--) {
            if (a[start] > a[i]) {
                temp = a[start];
                a[start] = a[i];
                a[i] = temp;
            } else
                continue;
        }
            start++;
    }
    for(int i = 0; i<a.length; i++){
        System.out.println(a[i]);
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值