zoukankan      html  css  js  c++  java
  • Java 数组

    声明数组变量

    /* 语法
    元素类型[] 数组名;   // 首选的方法
    或
    元素类型 数组名[];  // 效果相同,但不是首选方法
    */
    int[] x;         // 首选的方法
    或
    int x[];         //  效果相同,但不是首选方法
    

    创建数组

    /* 语法
    元素类型[] 数组名 = new 元素类型[元素个数或数组长度];
    */
    int[] x = new int[3];
    int[] x = {1, 2, 3};
    

    处理数组

    获取数组中的元素

    /*
    数组中有一个属性(.length)可以直接获取到数组元素个数。使用方式:数组名称.length = 
    获取数组中的元素。通常会用到遍历。
    */
    class Test{
        public static void main(String[] args){
    	int[] arr = {3, 6, 5, 1, 8, 9, 67};
    	printArray(arr);
        }
        //定义功能,用于打印数组中的元素。元素间用逗号隔开。
        public static void printArray(int[] arr){
    	System.out.print("arr = [");
    	for(int x = 0; x < arr.length; x++){
    	    if(x != arr.length - 1){
    	    System.out.print(arr[x] + ", ");
                }        
    	    else{
    	    System.out.println(arr[x] + "]");
    	    }		
    	}
        }
    }
    

    运行结果:
    arr = [3, 6, 5, 1, 8, 9, 67]

    获取数组中的最大值

    
    
    class Test 
    {
    	public static int getMax(int[] arr)
    	{
    		int max = arr[0];
    		for(int x=1; x<arr.length; x++){
    			if(arr[x]>max){
    				max = arr[x];
    			}
    		}
    		return max;
    	}
    	/*
    	另一方法:
    	将局部变量初始化为0,即初始化为数组中任意一个角标。	
    	public static int getMax_2(int[] arr)
    	{
    		int max = 0;
    		for(int x=1; x<arr.length; x++){
    			if(arr[x]>arr[max]){
    				max = x;
    			}
    		}
    		return arr[max];
    	}
    	*/
    	public static void main(String[] args)
    	{
    		int[] arr ={5,1,6,4,2,8,9};
    		int max = getMax(arr);
    		System.out.println("max = " + max);
    	}
    }
    
    

    对数组进行排序

    class Test {
        /*
        选择排序。
        内循环结束一次,最值出现头角标位置上。
        */
        public static void selectSort(int[] arr) {
            for (int x = 0; x < arr.length - 1; x++) {
                for (int y = x + 1; y < arr.length; y++) {
                    if (arr[x] > arr[y]) {
                        swap(arr, x, y);
                    }
                }
            }
        }
    
        /*
        冒泡排序
        public static void bubbleSort(int[] arr)
        {
            for(int x=0; x<arr.length-1; x++)
            {									
                 //-x:让每一次比较的元素减少,-1:避免角标越界。
                 for(int y=0; y<arr.length-x-1; y++){            
                    if(arr[y]<arr[y+1]){                              
                        swap(arr,y,y+1);
                    }
                }
            }
        }*/
        public static void swap(int[] arr, int a, int b) {
            int temp = arr[a];
            arr[a] = arr[b];
            arr[b] = temp;
        }
    
        public static void main(String[] args) {
            int[] arr = {5, 1, 6, 4, 2, 8, 9};
            //排序前;
            printArray(arr);
            //排序
            selectSort(arr);
            //bubbleSort(arr);
            //排序后:
            printArray(arr);
        }
    
        public static void printArray(int[] arr) {
            System.out.print("[");
            for (int x = 0; x < arr.length; x++) {
                if (x != arr.length - 1) {
                    System.out.print(arr[x] + ", ");
                } else {
                    System.out.println(arr[x] + "]");
                }
            }
        }
    }
    
    

    运行结果:
    [5, 1, 6, 4, 2, 8, 9]
    [1, 2, 4, 5, 6, 8, 9]

    反转数组

    class Test {
        public static void main(String[] args) {
            int[] arr = {3, 1, 5, 6, 2};
            printArray(arr);
            reverseArray(arr);
            //反转后
            printArray(arr);
        }
        //反转数组
        public static void reverseArray(int[] arr) {
            for (int start = 0, end = arr.length - 1; start < end; start++, end--) {
                swap(arr, start, end);
            }
        }
        //置换变量的值
        public static void swap(int[] arr, int a, int b) {
            int temp = arr[a];
            arr[a] = arr[b];
            arr[b] = temp;
        }
        //打印数组元素
        public static void printArray(int[] arr) {
            System.out.print("[");
            for (int x = 0; x < arr.length; x++) {
                if (x != arr.length - 1)
                    System.out.print(arr[x] + ", ");
                else
                    System.out.println(arr[x] + "]");
            }
        }
    }
    

    运行结果:
    [3, 1, 5, 6, 2]
    [2, 6, 5, 1, 3]

    二维数组

    class Test {
        public static void main(String[] args) {
            //int[] arr = new int[3]; 一维数组。
            int[][] arr = {{3, 5}, {2, 5}};//定义了名称为arr的二维数组,其中有2个一维数组。每一个一维数组中有两个元素。
            int sum = 0;
            // 计算所有元素的总和
            for (int x = 0; x < arr.length; x++) {//控制一维数组。
                for (int y = 0; y < arr[x].length; y++) {//控制二维数组。
                    sum = sum + arr[x][y];
                }
            }
            System.out.println("sum = " + sum);
        }
    }
    

    运算结果:
    sum=15

    编译出错

    • ArrayIndexOutOfBoundsException: 操作数组时,访问到了数组中不存在的角标。
    • NullPointerException:空指针异常:当引用没有任何指向值为null的情况,该引用还在用于操作实体。
  • 相关阅读:
    MySQL >>> 存储引擎
    MySQL >>> 基本操作语句
    MySQL >>> 使用安装
    协程 *单线程实现并发
    进程池 & 线程池
    线程 *知识点扩充
    进程 & 线程
    DRF单表序列化
    DRF
    接口规范
  • 原文地址:https://www.cnblogs.com/hen-java/p/12599513.html
Copyright © 2011-2022 走看看