数组学习

一、综述

       数组代表一系列对象或者基本数据类型,所有相同的类型都封装到一起——采用一个统一的标识符名称。
       数组的创建实际是在运行期间进行的。

二、定义和初始化

       数组的定义和使用是通过方括号索引运算符进行的([ ])
       e.g.1       int[ ] al;
                       int al[ ];
              这时只是定义了指向数组的一个句柄,而且尚未给数组分配任何空间。
              为了给数组创建相应的存储空间,必须编写一个初始化表达式。
       e.g.2       int[] a1 = { 1, 2, 3, 4, 5 };

三、用法Example(含讲解)

       1、复制句柄

public class Arrays {
  public static void main(String[] args) {
    int[] a1 = { 1, 2, 3, 4, 5 };
    int[] a2;
    a2 = a1;
    for(int i = 0; i < a2.length; i++)
      a2[i]++;
    for(int i = 0; i < a1.length; i++)
      prt("a1[" + i + "] = " + a1[i]);
  }
  static void prt(String s) {
    System.out.println(s);
  }
}

(1)a1获得了一个初始值,而a2没有;a2将在以后赋值——这种情况下是赋给另一个数组。
(2)length: 所有数组都有这个本质成员(无论它们是对象数组还是基本类型数组),可以用它来获知数组内包含了多少个元素。
                           由于Java数组从元素0开始计数,所以能索引的最大元素编号是“length-1”。

       2、创建一个非基本类型对象的数组

import java.util.*;

public class ArrayClassObj {
  static Random rand = new Random();
  static int pRand(int mod) {
    return Math.abs(rand.nextInt()) % mod + 1;
  }
  public static void main(String[] args) {
    Integer[] a = new Integer[pRand(20)];
    prt("length of a = " + a.length);
    for(int i = 0; i < a.length; i++) {
      a[i] = new Integer(pRand(500));
      prt("a[" + i + "] = " + a[i]);
    }
  }
  static void prt(String s) {
    System.out.println(s);
  }
}

(1)在new调用后才开始创建数组:Integer[] a = new Integer[pRand(20)];
(2)它只是一个句柄数组,而且除非通过创建一个新的Integer对象,从而初始化了对象句柄,否则初始化进程不会结束:a[i] = new Integer(pRand(500));
           但若忘记创建对象,就会在运行期试图读取空数组位置时获得一个“违例”错误。

       3、初始化对象数组一(Java 1.0允许的唯一形式);
             初始化对象数组二(自Java 1.1才开始提供支持);

public class ArrayInit {
  public static void main(String[] args) {
    Integer[] a = {
      new Integer(1),
      new Integer(2),
      new Integer(3),
    };

    // Java 1.1 only:
    Integer[] b = new Integer[] {
      new Integer(1),
      new Integer(2),
      new Integer(3),
    };
  }
}

(1)这种做法大多数时候都很有用,但限制也是最大的,因为数组的大小是在编译期间决定的。
(2)初始化列表的最后一个逗号是可选的(这一特性使长列表的维护变得更加容易)。

       4、Object数组
              由于所有类最终都是从通用的根类Object中继承的,所以能创建一个方法,令其获取一个Object数组

// Using the Java 1.1 array syntax to create
// variable argument lists

class A { int i; }

public class VarArgs {
  static void f(Object[] x) {
    for(int i = 0; i < x.length; i++)
      System.out.println(x[i]);
  }
  public static void main(String[] args) {
    f(new Object[] {
        new Integer(47), new VarArgs(),
        new Float(3.14), new Double(11.11) });
    f(new Object[] {"one", "two", "three" });
    f(new Object[] {new A(), new A(), new A()});
  }
}

       5、多维数组

//: MultiDimArray.java
// Creating multidimensional arrays.
import java.util.*;

public class MultiDimArray {
  static Random rand = new Random();
  static int pRand(int mod) {
    return Math.abs(rand.nextInt()) % mod + 1;
  }
  public static void main(String[] args) {
    int[][] a1 = {
      { 1, 2, 3, },
      { 4, 5, 6, },
    };
    for(int i = 0; i < a1.length; i++)
      for(int j = 0; j < a1[i].length; j++)
        prt("a1[" + i + "][" + j +
            "] = " + a1[i][j]);
    // 3-D array with fixed length:
    int[][][] a2 = new int[2][2][4];
    for(int i = 0; i < a2.length; i++)
      for(int j = 0; j < a2[i].length; j++)
        for(int k = 0; k < a2[i][j].length;
            k++)
          prt("a2[" + i + "][" +
              j + "][" + k +
              "] = " + a2[i][j][k]);
    // 3-D array with varied-length vectors:
    int[][][] a3 = new int[pRand(7)][][];
    for(int i = 0; i < a3.length; i++) {
      a3[i] = new int[pRand(5)][];
      for(int j = 0; j < a3[i].length; j++)
        a3[i][j] = new int[pRand(5)];
    }
    for(int i = 0; i < a3.length; i++)
      for(int j = 0; j < a3[i].length; j++)
        for(int k = 0; k < a3[i][j].length;
            k++)
          prt("a3[" + i + "][" +
              j + "][" + k +
              "] = " + a3[i][j][k]);
    // Array of non-primitive objects:
    Integer[][] a4 = {
      { new Integer(1), new Integer(2)},
      { new Integer(3), new Integer(4)},
      { new Integer(5), new Integer(6)},
    };
    for(int i = 0; i < a4.length; i++)
      for(int j = 0; j < a4[i].length; j++)
        prt("a4[" + i + "][" + j +
            "] = " + a4[i][j]);
    Integer[][] a5;
    a5 = new Integer[3][];
    for(int i = 0; i < a5.length; i++) {
      a5[i] = new Integer[3];
      for(int j = 0; j < a5[i].length; j++)
        a5[i][j] = new Integer(i*j);
    }
    for(int i = 0; i < a5.length; i++)
      for(int j = 0; j < a5[i].length; j++)
        prt("a5[" + i + "][" + j +
            "] = " + a5[i][j]);
  }
  static void prt(String s) {
    System.out.println(s);
  }
}

(1)第一个例子展示了基本数据类型的一个多维数组。我们可用花括号定出数组内每个矢量的边界:
          int[][] a1 = {
            { 1, 2, 3, },
            { 4, 5, 6, },
          };
(2)第二个例子展示了用new分配的一个三维数组。在这里,整个数组都是立即分配的。
(3)第三个例子向大家揭示出构成矩阵的每个矢量都可以有任意的长度:
         int[][][] a3 = new int[pRand(7)][][];
         for(int i = 0; i < a3.length; i++) {
           a3[i] = new int[pRand(5)][];
           for(int j = 0; j < a3[i].length; j++)
             a3[i][j] = new int[pRand(5)];
         }
(4)第四个例子,它向我们演示了用花括号收集多个new表达式的能力。
(5)第五个例子展示了如何逐渐构建非基本类型的对象数组
          i*j只是在Integer里置了一个有趣的值。

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值