• Java数组(1):数组与多维数组


    我们对数组的基本看法是,你可以创建它们,通过使用整型索引值访问它们的元素,并且他们的尺寸不能改变。

    但是有时候我们需要评估,到底是使用数组还是更加灵活的工具。数组是一个简单的线性序列,这使得元素访问非常快速。但是为这种速度所付出的代价是数组大小被固定,并且在其生命周期中不可改变。

    无论使用哪种类型的数组,数组标识符只是一个引用。基本类型数组直接保存基本类型的值,对象数组保存一个指向其他对象的引用。

     1 class Apple {
     2     private static int count = 1;
     3 
     4     public Apple() {
     5         System.out.println("Apple" + count++);
     6     }
     7 }
     8 
     9 public class ArrayOptions {
    10     public static void main(String[] args) {
    11         Apple[] a; // 创建一个Apple类型的数组引用
    12         Apple[] b = new Apple[5]; // 创建一个Apple类型的数组对象,但是都指向null
    13         System.out.println("b: " + Arrays.toString(b)); // b: [null, null, null, null, null]
    14         Apple[] c = new Apple[4];
    15         for (int i = 0; i < c.length; i++)
    16             c[i] = new Apple(); // Apple1 Apple2 Apple3 Apple4
    17         // Aggregate initialization:
    18         Apple[] d = {new Apple(), new Apple(), new Apple()}; // Apple5 Apple6 Apple7
    19         // Dynamic aggregate initialization:
    20         a = new Apple[]{new Apple(), new Apple()}; // Apple8 Apple9
    21         System.out.println("a.length = " + a.length); // a.length = 2
    22         System.out.println("b.length = " + b.length); // b.length = 5
    23         System.out.println("c.length = " + c.length); // c.length = 4
    24         System.out.println("d.length = " + d.length); // d.length = 3
    25         a = d;
    26         System.out.println("a.length = " + a.length); // a.length = 3
    27 
    28         int[] e; // 创建一个基本类型的数组引用
    29         int[] f = new int[5]; // 创建一个Apple基本数组对象,初始化值都为0
    30         System.out.println("f: " + Arrays.toString(f)); // f: [0, 0, 0, 0, 0]
    31         int[] g = new int[4];
    32         for (int i = 0; i < g.length; i++)
    33             g[i] = i * i;
    34         int[] h = {11, 47, 93};
    35         System.out.println("f.length = " + f.length); // f.length = 5
    36         System.out.println("g.length = " + g.length); // g.length = 4
    37         System.out.println("h.length = " + h.length); // h.length = 3
    38         e = h;
    39         System.out.println("e.length = " + e.length); // e.length = 3
    40         e = new int[]{1, 2};
    41         System.out.println("e.length = " + e.length); // e.length = 2
    42     }
    43 }

    多维数组

     1 import java.util.Arrays;
     2 
     3 class Apple {
     4     private static int count = 1;
     5 
     6     public String toString() {
     7         return "Apple" + count++;
     8     }
     9 }
    10 
    11 public class MutiArray {
    12     public static void main(String[] args) {
    13         // (1) 使用Arrays.deepToString()可以打印多维数组
    14         int[][] a = {{1, 2, 3}, {4, 5, 6}};
    15         long[][][] b = new long[2][2][3];
    16         System.out.println(Arrays.deepToString(a)); // [[1, 2, 3], [4, 5, 6]]
    17         System.out.println(Arrays.deepToString(b)); // [[[0, 0, 0], [0, 0, 0]], [[0, 0, 0], [0, 0, 0]]]
    18 
    19         // (2) 多维数组值的设定可以用下面方法,并且数组中构成矩阵的每个向量都可具有任意长度(粗糙数组)
    20         int[][] c = new int[3][];
    21         for (int i = 0; i < c.length; i++) {
    22             c[i] = new int[i + 1];
    23             for (int j = 0; j < c[i].length; j++) {
    24                 c[i][j] = j + 1;
    25             }
    26         }
    27         System.out.println(Arrays.deepToString(c)); // [[1], [1, 2], [1, 2, 3]]
    28 
    29         // (3) 用类似方式还可以处理非基本类型的(包装类和对象类)数组
    30         Apple[][] d = {{new Apple(), new Apple()}, {new Apple(), new Apple(), new Apple()}};
    31         Integer[][] e = {{1, 2}, {3, 4, 5}, {6, 7}};
    32         System.out.println(Arrays.deepToString(d)); // [[Apple1, Apple2], [Apple3, Apple4, Apple5]]
    33         System.out.println(Arrays.deepToString(e)); // [[1, 2], [3, 4, 5], [6, 7]]
    34     }
    35 }
  • 相关阅读:
    Oracle client 安装、配置
    js 计算金额是否小于总金额,大于是不能保存
    SQL Server函数与存储过程 计算时间
    sql Server 创建临时表 嵌套循环 添加数据
    SQL Server 事务日志文件已满,收缩日志文件(9002)
    sql Server 2008 数据库自动备份维护计划
    面向对象的三个基本特征:继承、封装、多肽
    MySQL 语句调优
    SQL 多表关联更新
    值类型、引用类型
  • 原文地址:https://www.cnblogs.com/storml/p/8401593.html
Copyright © 2020-2023  润新知