【JAVA】 容纳对象 数组

数组的特点

a) 数组将索引与对象关联在一起。
b) 数组保存已知类型的对象,这样在查找对象时就不必强制转换结果。
c) 数组可以是多维的,并且可以保存原始类型。
d) 但是,一旦创建数组,就不能更改其大小。

方法

  1. charAt
    public char charAt(int index)
    返回指定索引处的字符值。索引的范围从0到length()-1。序列的第一个字符值在 索引0处,下一个在索引1处,依此类推,用于数组索引。
  2. length
    public int length()
    返回此字符序列的长度。
  3. subSequence
    public CharSequence subSequence(int start, int end)
    返回此序列的子序列CharSequence。子序列以指定索引处的char值开始,以索引end-1处的char值结束。返回序列的长度(以字符为单位)是end-start,因此如果start==end,则返回空序列。
  4. clone
    public Object clone()
    创建一个浅副本
    (浅拷贝是一组指向相同存储位置的指针。实际上,它不会创建真实副本,因此内存使用率较低。在深度复制的情况下,将创建内存段的精确副本,并将指针设置到新的内存位置。因此,从理论上讲,在这种情况下,内存消耗应为两倍。)
  5. toString
    public String toString()
    转化成字符串

实例

//: ArraySize.java
// Initialization & re-assignment of arrays
package c08;

class Weeble {} // A small mythical creature

public class ArraySize {
  public static void main(String[] args) {
    // Arrays of objects:
    Weeble[] a; // Null handle
    Weeble[] b = new Weeble[5]; // Null handles
    Weeble[] c = new Weeble[4];
    for(int i = 0; i < c.length; i++)
      c[i] = new Weeble();
    Weeble[] d = {
      new Weeble(), new Weeble(), new Weeble()
    };
    // Compile error: variable a not initialized:
    //!System.out.println("a.length=" + a.length);
    System.out.println("b.length = " + b.length);
    // The handles inside the array are 
    // automatically initialized to null:
    for(int i = 0; i < b.length; i++)
      System.out.println("b[" + i + "]=" + b[i]);
    System.out.println("c.length = " + c.length);
    System.out.println("d.length = " + d.length);
    a = d;
    System.out.println("a.length = " + a.length);
    // Java 1.1 initialization syntax:
    a = new Weeble[] {
      new Weeble(), new Weeble()
    };
    System.out.println("a.length = " + a.length);

   // Arrays of primitives:
   int[] e; // Null handle
   int[] f = new int[5];
   int[] g = new int[4];
   for(int i = 0; i < g.length; i++)
     g[i] = i*i;
   int[] h = { 11, 47, 93 };
   // Compile error: variable e not initialized:
   //!System.out.println("e.length=" + e.length);
   System.out.println("f.length = " + f.length);
   // The primitives inside the array are
   // automatically initialized to zero:
   for(int i = 0; i < f.length; i++)
     System.out.println("f[" + i + "]=" + f[i]);
   System.out.println("g.length = " + g.length);
   System.out.println("h.length = " + h.length);
   e = h;
   System.out.println("e.length = " + e.length);
   // Java 1.1 initialization syntax:
   e = new int[] { 1, 2 };
   System.out.println("e.length = " + e.length);
  }
} ///:~
Here’s the output from the program:

b.length = 5
b[0]=null
b[1]=null
b[2]=null
b[3]=null
b[4]=null
c.length = 4
d.length = 3
a.length = 3
a.length = 2
f.length = 5
f[0]=0
f[1]=0
f[2]=0
f[3]=0
f[4]=0
g.length = 4
h.length = 3
e.length = 3
e.length = 2

注意:
数组a只是初始化成一个null句柄。编译器会禁止对这个句柄作任何实际操作,除非已正确地初始化了它。
数组b被初始化成指向由Weeble句柄构成的一个数组,虽然里并未放置任何Weeble对象,但是仍然可以查询该数组的大小。
new Weeble[5]指定了数组对象的大小或容量,但是不知道里面容纳了多少个元素。尽管如此,由于数组对象在创建之初会自动初始化成null,所以可检查它是否为null,判断一个特定的数组“空位”是否容纳一个对象。类似地,由基本数据类型构成的数组会自动初始化成零(针对数值类型)、null(字符类型)或者false(布尔类型)。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值