包装类与数组

Terwer Java SE评论191字数 1469阅读4分53秒阅读模式
  1. 包装(Wrapper Class)。

    针对原生数据类型的包装。 包装类(8 个)都位于 java.lang​ 包下。文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

    java 中的 8 个包装类分别是:Byte,Short,Integer,Long,Float,Double,Character,Boolean。文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

    他们的使用方式都是一样的,可以实现原生数据类型和包装类额双向转换。文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

  2. 数组(Array):相同类型 数据的集合叫做数组。文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

  3. 如何定义数组:文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

    type[] 变量名 = new type[数组中元素的个数 ]; 
    

    按照下列方式定义长度为 10 的数组:文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

    int[] a = new int[10]; // 或者
    int a[] = new int[10];
    
  4. 数组中的元素索引是从 0 开始的。对于数组来说,最大的索引 == 数组的长度 - 1 。文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html
  5. 定义数组的第三种方式:文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

    type[] 变量名 = new type[]{逗号分隔的初始化值列表}
    
  6. Java 中的每个数组都有一个 length 属性,他表示数组的长度。

    length 属性是 public,final,int 的。数组长度一旦确定,就不能改变大小。文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

  1. 两个数组的比较不能使用 ==​ ,也不能使用 equals​ ,要使用 Arrays.equals​ 。文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

    int[] a = {1, 2, 3};
    int[] b = {1, 2, 3};
    System.out.println(a==b);
    System.out.println(a.equals(b));
    System.out.println(Arrays.equals(a, b));
    
    // false
    // false
    // true
    
  2. int a[] = new int[10]​ ,其中 a 是一个引用。它指向了上次的数组对象的首地址,数组中每个元素都是 int 类型,其中仅放数据值本身。

    包装类与数组文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

  3. 下列代码的内存布局文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

    Person[] p = new Person[3];
    
    p[0] = new Person(10);
    p[1] = new Person(20);
    p[2] = new Person(30);
    

    包装类与数组文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

  4. 二维数组。二维数组是一种平面的二维结构,本质上是数组的数组。文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

    例如下列数组,实际的存储文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

    ```java
    int[][] i = new int[2][3];文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

    System.out.println(i instanceof Object);
    System.out.println(i[0] instanceof Object);
    System.out.println(i[0] instanceof int[]);文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

    // true
    // true
    // true
    ```文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

    包装类与数组文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

  5. 二维数组中初始化为偶数文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

    ```java
    int idx = 0;
    for (int k = 0; k < 2; k++) {
    for (int j = 0; j < 3; j++) {
    i[k][j] = 2 * (idx + 1);
    idx++;
    }
    }文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

    for (int[] ints : i) {
    for (int anInt : ints) {
    System.out.println(anInt);
    }
    }
    ```文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

    如图:文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

    包装类与数组文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

  6. 二维数组的定义和初始化文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

    ```java
    /*
    int[][] a = new int[3][];
    a[0] = new int[2];
    a[1] = new int[3];
    a[2] = new int[1];
    */文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

    // int[][] a = new int[][3];文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

    int[][] a = new int[][]{{1, 2, 3}, {4, 5}};文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

    for (int[] ints : a) {
    for (int anInt : ints) {
    System.out.print(anInt + " ");
    }
    System.out.println();
    }文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

    // 1 2 3
    // 4 5
    ```文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

  7. Arrays文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

    Arrays.equals文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html

  8. System.arrayCopy
  9. 三维数组。type[][][] a = new type[2][3][4]
文章源自浅海拾贝-https://blog.terwergreen.com/packaging-class-and-array-2l6rfo.html
相关文章
  • 扫码加我微信
  • 验证消息请输入:来自你的博客
  • weinxin
  • 我的微信公众号
  • 微信扫一扫与我交流吧
  • weinxin
Terwer
匿名

发表评论

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen: