关键词搜索

源码搜索 ×
×

(Java)第 4 章 数组与方法:数组的定义及使用

发布2019-10-22浏览653次

详情内容

一、一维数组

一维数组的声明与分配内存

数据类型 数组名称[] = null; //声明数组
数组名称 = new 数据类型[长度]; //分配内存给数组

    数组声明的另外一种形式

    数据类型[] 数组名称 = null; //声明一维数组
    
    • 1

    声明数组的同时分配内存

    数据类型 数组名[] = new 数据类型[个数];
    
    • 1

    示例:

    int score[] = null;
    score = new int[3];
    等价于
    int score[] = new int[10]; 
    
      3
    • 4

    数组声明的疑问?

    数组的声明上为什么要写上一个“null”呢?例如:“int score[] = null;”
    数组属于引用数据类型,那么对于引用数据类型来说其默认值就是null,表示暂时还没有任何指向的内存空间。

    public class ArrayDemo01 {
        public static void main(String[] args) {
            int score[] = new int[4];
            for (int x=0;x<4;x++){
                score[x] = x*2+1;
            }
            //返回数组的长度
            System.out.println("The length of the array is :" + score.length);
        }
    }
    
      3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10

    关于内存分配的操作
    在这里插入图片描述
    从图中可以发现,一个数组开辟了堆内存之后,将在堆内存中保存数据,并将堆内存的操作地址给了数组名称score。
    在这里插入图片描述

    二、数组的静态初始化

    数组赋初值

    数据类型 数组名[] = {初值0,初值1,....初值n}
    例如:
    int score[] = {32,45,67,90};
    
      3

    示例:由小到大排序

    package Chapter_4;
    
    public class ArrayDemo01 {
        public static void main(String[] args) {
            int score[] = {67,89,69,100,75};
            for(int i=0;i<score.length;i++){
                for (int j=i;j<score.length;j++){
                    if(score[i]>score[j]){
                        int temp = score[j];
                        score[j] = score[i];
                        score[i] = temp;
                    }
                }
                System.out.print("第"+i+"次排序的结果:\t");
                for(int j=0;j<score.length;j++){
                    System.out.print(score[j]+"\t");
                }
                System.out.println("");
            }
            System.out.print("最终的排序结果为:\t");
            for (int i=0;i<score.length;i++){
                System.out.print(score[i]+"\t");
            }
    
        }
    }
    
      3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26

    三、二维数组

    二维数组的声明格式

    数据类型 数组名[][];
    数组名 = new 数据类型[行的列数][列的个数];
    

      与一位数组不同,二维数组在分配内存时,必须告诉编译器二维数组行与列的个数
      示例:

      int score[][];//声明整型数组 score
      score = new int[3][4];//配置一块内存空间,供3行4列的整型数组score使用
      

        简介的声明方式:

        数据类型 数组名[][] = new 数据类型[行的个数][列的个数];
        示例:
        int score[][] = new int[3][4];//声明整型数组score,同时为其开辟一块内存空间
        
          3

        二维数组静态初始化格式

        数据类型 数组名[][] = {
        	{0行初值},
        	{1行初值},
        	....
        	{第n行初值},
        };
        
          3
        • 4
        • 5
        • 6

        示例:

        public class ArrayDemo01 {
            public static void main(String[] args) {
                int score[][] = {{67,42},{45,67,89},{99,100,88}};
                for (int i = 0;i<score.length;i++){
                    for (int j=0;j<score[i].length;j++){
                        System.out.print(score[i][j] + "\t");
                    }
                    System.out.println("");
                }
            }
        }
        
          3
        • 4
        • 5
        • 6
        • 7
        • 8
        • 9
        • 10
        • 11

        在这里插入图片描述

        三、补充

        int[] a;
        
        • 1

        这里拥有的只是对数组的一个引用(已经为该引用分配了足够的存储空间),但是还没有给数组对象本身分配任何空间。为了给数组创建相应的存储空间,必须写初始化表达式。

        int[] a = {1,2,3,4};
        或者
        int[] a = null;
        a = new int[3];
        
          3
        • 4

        相关技术文章

        点击QQ咨询
        开通会员
        返回顶部
        ×
        微信扫码支付
        微信扫码支付
        确定支付下载
        请使用微信描二维码支付
        ×

        提示信息

        ×

        选择支付方式

        • 微信支付
        • 支付宝付款
        确定支付下载