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

    数组的定义

    • 数组是相同数据类型的有序集合
    • 数组描述的是相同类型的若干个数据,按照一定的先后次序排列组合而成
    • 其中,每一个数据陈作一个数组的元素,每个数组元素可以通过一个下标来访问他们。

    数组声明创建

    • 首先必须声明数组变量,才能在程序中使用数组。

      一下为数组定义的格式

    //数组的定义
    数组的类型[] 数组的名字;
    int[] nums;//首选
    
    int nums2[];//了解
    
    • Java语言使用new操作符来创建数组,语法如下
    int[] nums;//声明一个数组
    nums=new int[10];//创建一个数组
    //这里面可以存放10个int类型的数字
    //也可以写成:int[] nums=new int[10];
    
    • 数组的元素是从0号元素开始的
    • 获取数组长度:
    arrays.length
    

    数组的三种初始化

    静态初始化

    //静态初始化:创建+赋值
    int[] a={10,20,30};
    

    动态初始化

    int[] b=new int[10];
    b[0]=10;
    b[1]=20;
    b[2]=30;
    

    数组的四个基本特点

    • 其长度是确定的,数组一旦被创建,它的的大小就是不可改变的

    • 其元素必须是相同类型,不允许出现混合类型

    • 数组中的元素可以是任何数据类型,包括基本类型和引用类型

    • 数组变量属于引用类型,数组也可以看成是对象,数组中的每个元素相当于该对象的成员变量。数组本身就是对象,Java中对象是在堆中,因此数组无论保存原始类型还是其他对象类型,数组对象本身是在堆中的。

    数组边界

    • 下标的合法区间:[0,length-1], 如果越界就会报错;
    • ArrayIndexOutOfBoundsException:数据下标异常

    数组的使用

    普通的for循环

    public static void main(String[] args){
        int[] ary={1,2,3,4,5};
        System.out.print("数:");
        for (int i = 0; i < ary.length; i++) {
            System.out.print(ary[i]+" ");
        }
        int sum=0;
        for (int i = 0; i <ary.length ; i++) {
            sum=sum+ary[i];
        }
        System.out.println("\n"+"和:"+sum);
        int max=ary[0];
        for (int i = 0; i < ary.length; i++) {
            if (ary[i]>ary[0]){
                max=ary[i];
            }
        }
        System.out.println("最大值"+max);
    }
    

    For-Each循环

    public static void main(String[] args) {
            //限jdk1.5之后使用
            int[] ary = {10, 20, 30, 40, 50};
            for (int arrays : ary) {
                System.out.println(arrays);
            }
        }
    

    数组作方法入参

        public static void main(String[] args) {
            int[] ary = {10, 20, 30, 40, 50};
            printArray(ary);
            System.out.println("\n" + "=======");
            reverse(ary);
        }
        //打印数据结构
        public static void printArray(int[] arrays) {
            for (int i = 0; i < arrays.length; i++) {
                System.out.print(arrays[i] + " ");
            }
        }
        //数组翻转
        public static void reverse(int[] arrays) {
            int[] result = new int[arrays.length];
            for (int i = 0, j = arrays.length - 1; i < arrays.length; i++, j--) {
                result[i] = arrays[j];
            }
            for (int i = 0; i < result.length; i++) {
                System.out.print(result[i] + " ");
            }
        }
    

    数组作为返回值

    public static void main(String[] args) {
            int[] ary = {10, 20, 30, 40, 50};
            int[] reverse = reverse(ary);
            for (int i = 0; i < reverse.length; i++) {
                System.out.print(reverse[i] + " ");
            }
        }
    
        public static int[] reverse(int[] arrays) {
            int[] result = new int[arrays.length];
            for (int i = 0, j = arrays.length - 1; i < arrays.length; i++, j--) {
                result[i] = arrays[j];
            }
            return result;
        }
    

    小结:

    • 数组是相同数据类型(数据类型可以为任意类型)的有序集合

    • 数组也是对象,数组元素相当于对象的成员

    • 数组长度是确定的,不可变的,如果越界,则报:ArrayIndexOutOfBoundsException

    • 即没有被赋值的元素,是有个默认值的,例如int类型的默认值为0

  • 相关阅读:
    IfcFlowDirectionEnum
    QAxWidget
    IfcDistributionFlowElementType
    IfcBuildingElementProxy 概念用法
    opencv图片旋转90度
    IfcMaterialList
    IfcDistributionChamberElement
    IfcArithmeticOperatorEnum
    FileWriter
    IfcDistributionChamberElementType
  • 原文地址:https://www.cnblogs.com/DL50/p/13065952.html
Copyright © 2011-2022 走看看