zoukankan      html  css  js  c++  java
  • Java(四)——数组

    一、遍历数组

    1、普通for循环遍历

    public static void main(String[] args){
        int[] arr = new int[]{1,2,3,4,5};
        // i为数组的索引
        for (int i =0; i < arr.length; i++){			// arr.length 表示 数组arr的长度
            System.out.println(arr[i]);				// 1                2                3                      4                         5  			// 一个元素一行
        }
    }
    

    2、for-each循环遍历

    直接迭代数组的每个元素,但是无法获取每个元素的索引

    public static void main(String[] args){
        int[] arr = new int[]{1,2,3,4,5};
        // ele为数组中的元素,而不是索引
        for(int ele : arr){
            System.out.println(ele); 		// 1                2                3                      4                         5  // 一个元素一行
        }
    }
    

    3、直接打印数组

    Arrays.toString(数组对象)

    public static void main(String[] args){
        int[] arr = new int[]{1,2,3,4,5};
        System.out.println(Arrays.toString(arr))    		// [1,2,3,4,5]
        }
    }
    

    二、数组排序

    常见的数组排序方式:选择排序、冒泡排序和快速排序

    1、选择排序

    依次获取每个元素,与后续的几个元素比较

    时间复杂度为O(n^2)

    // 从小到大排序,arr[0]经过一轮比较变成最小的值
    //  arr[i] 依次与后续的每一个元素比较,arr[i]始终为两个元素比较中较小的值
    public static void main(String[] args){
        int[] arr = new int[]{11, 4, 2, 3, 6};
        for (int i = 0; i < arr.length; i++) {
            for (int j = i + 1; j < arr.length; j++) {
                if (arr[i] > arr[j]) {
                    int temp = arr[i];
                    arr[i] = arr[j];
                    arr[j] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(arr));          //  {2,3,4,5,11}
    }
    
    

    2、冒泡排序

    冒泡法是通过遍历数组进行比较,通过不断的比较将最小值或者最大值一个一个的遍历出来

    时间复杂度为O(n^2)

    public static void main(String[] args){
        int[] arr = new int[]{11, 4, 2, 3, 6};
        // 相邻两个元素比较,arr[i] 只能取到倒数第二个元素,否则
        for (int i = 0; i < arr.length-1; i++) {
            for (int j =0; j < arr.length-1-i; j++) {
                if(arr[j]>arr[j+1]){
                    int temp = arr[j];
                    arr[j] = arr[j+1];
                    arr[j] = temp;
                }
            }
        }
        System.out.println(Arrays.toString(arr));          //  {2,3,4,5,11}
    }
    
    

    3、快速排序

    时间复杂度为O(n*logn)

    // Arrays.sort(obj) 即为快速排序
    int[] arr = new int[]{11, 4, 2, 3, 6};
    Arrays.sort(arr);
    System.out.println(Arrays.toString(arr));
    
    public static void main(String[] args){
        int[] arr={4,2,9,3,5,8,7,1,6};
        int start=0;
        int end=arr.length-1;
        quickSort(arr,start,end);
        printArray(arr);
    }
    
    
    public static void quickSort(int[] arr,int low,int high){
        int start=low;
        int end=high;
        int key=arr[low];
        while(start<end){
            //从后往前开始比较
            while(end>start && arr[end]>=key)//如果没有比关键值小的,比较下一个,直到有比关键值小的交换位置,然后又从前往后比较
                end--;
            if(arr[end]<=key){
                int temp=arr[end];
                arr[end]=arr[start];
                arr[start]=temp;
            }
            //从前往后比较
            while(end>start && arr[start]<=key)//如果没有比关键值大的,比较下一个,直到有比关键值大的交换位置,然后又从后往前比较
                start++;
            if(arr[start]>=key){
                int temp=arr[start];
                arr[start]=arr[end];
                arr[end]=temp;	
            }
            //此时第一次循环比较结束,关键值的位置已经确定了。左边的值都比关键值小,右边的值都比关键值大,但是两边的顺序还有可能是不一样的,进行下面的递归调用
        }
        //递归
        if(start>low) 
            quickSort(arr,low,start-1);
        if(start<high) 
            quickSort(arr,end+1,high);
    
    }
    

    三、二维数组

    1、遍历

    // 普通for循环遍历
    int [][] arr = {{1,2,3}, {2,3,1}, {1,2}};
    for(int i=0; i<arr.length; i++){
        for(int j=0; j<arr[i].length; j++){
            System.out.println(arr[i][j]);
        }
    }
    
    // for each遍历
    int [][] arr = {{1,2,3}, {2,3,1}, {1,2}};
    for(int[] wrapper: arr){
        for (int ele:wrapper){
            System.out.println(ele);
        }
    }
    

    2、打印二维数组

    // Arrays.deepToString(obj)  直接打印二位数组
    int [][] arr = {{1,2,3}, {2,3,1}, {1,2}};
    System.out.println(Arrays.deepToString(arr));    // {{1,2,3}, {2,3,1}, {1,2}}
    
  • 相关阅读:
    12 Source Code Profilers for C & C++
    HttpWebRequest的使用方法
    MSDN Windows 下载
    Qt 4.7 在VS2010环境下的编译
    [转].NET Logging Tools and Libraries
    硬盘崩溃之后
    .net core 下使用 logdashboard 日志面板
    工具收藏 年终工作总结必备工具之ppt利器
    Dapper 的应用和Dapper.Contrib 的方法封装(一)
    Dapper 的应用和Dapper.Contrib 的方法封装(二)
  • 原文地址:https://www.cnblogs.com/linagcheng/p/12158332.html
Copyright © 2011-2022 走看看