Java数组的深入研究

数组的特殊之处

  • Java编程思想上有:数组与其他种类的容器之间的区别有三方面 —— 效率、类型和保存基本类型的能力。
    • 数组是一种效率最高的存储和随机访问对象引用序列 的方式 —— 与之相应的代价:数组对象的大小被固定,并且其声明周期不可改变
  • 数组可以持有基本数据类型,而泛型之前的容器则不能。 —— 但有了泛型,容器就可以指定并检查它们所持有对象的类型。
数组与泛型容器的比较(见下面代码):
package com.chenny.Entity;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

class BerylliumSphere {
    private static long counter;
    private final long id = counter++;
    public String toString(){
        return "Sphere" + id;
    }
}

public class ContainerComparison {
    public static void main(String[] args) {
        BerylliumSphere[] spheres = new BerylliumSphere[10];
        for (int i = 0;i < 5; i++){
            spheres[i] = new BerylliumSphere();
        }
        //Arrays的查看方法
        System.out.println(Arrays.toString(spheres));
        System.out.println(spheres[4]);

        System.out.println("======================================================================");

        //用List存储查看
        List<BerylliumSphere> sphereList = new ArrayList<BerylliumSphere>();
        for(int i = 0; i < 5; i++){
            sphereList.add(new BerylliumSphere());
        }
        System.out.println(sphereList);
        System.out.println(sphereList.get(4));

        System.out.println("======================================================================");
        
        
        int[] integers = {0, 1, 2, 3, 4, 5};
        System.out.println(Arrays.toString(integers));
        System.out.println(integers[4]);
        
        List<Integer> intList = new ArrayList<Integer>(Arrays.asList(0,1,2,3,4,5));
        intList.add(97);
        
        System.out.println(intList);
        System.out.println(intList.get(4));
    }
}

一维数组

package com.chenny.Entity;

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;

class BerylliumSphere {
    private static long counter;
    private final long id = counter++;
    public String toString(){
        return "Sphere" + id;
    }
}

public class ArrayOptions {
    public static void main(String[] args) {
        // Arrays of objects:
        BerylliumSphere[] a; // 局部未初始化变量
        BerylliumSphere[] b = new BerylliumSphere[5];
        // The references inside the array are
        // automatically initialized to null:
        print("b: " + Arrays.toString(b));

        BerylliumSphere[] c = new BerylliumSphere[4];
        for(int i = 0; i < c.length; i++)
            if(c[i] == null) // Can test for null reference
                c[i] = new BerylliumSphere();
        // 聚合初始化:
        BerylliumSphere[] d = { new BerylliumSphere(), new BerylliumSphere(), new BerylliumSphere()};
        System.out.println(Arrays.toString(d));

        // 动态聚合初始化:
        a = new BerylliumSphere[]{new BerylliumSphere(), new BerylliumSphere(),};
        System.out.println(Arrays.toString(a));

        // (两种情况下尾随逗号都是可选的)
        print("a.length = " + a.length);
        print("b.length = " + b.length);
        print("c.length = " + c.length);
        print("d.length = " + d.length);
        a = d;
        print("a.length = " + a.length);

        //基元数组
        int[] e; // 空引用
        int[] f = new int[5];

        // The primitives inside the array are
        // automatically initialized to zero:
        //数组中的基元是
        //自动初始化为零:
        print("f: " + Arrays.toString(f));
        int[] g = new int[4];
        for(int i = 0; i < g.length; i++)
            g[i] = i*i;
        int[] h = { 11, 47, 93 };

        //编译错误:变量e未初始化:
        //!print("e.length = " + e.length);
        print("f.length = " + f.length);
        print("g.length = " + g.length);
        print("h.length = " + h.length);
        e = h;
        print("e.length = " + e.length);
        e = new int[]{ 1, 2 };
        print("e.length = " + e.length);
    }

    private static void print(String s) {
        System.out.println(s);
    }
}
/*结果如下:
b: [null, null, null, null, null]
        [Sphere4, Sphere5, Sphere6]
        [Sphere7, Sphere8]
        a.length = 2
        b.length = 5
        c.length = 4
        d.length = 3
        a.length = 3
        f: [0, 0, 0, 0, 0]
        f.length = 5
        g.length = 4
        h.length = 3
        e.length = 3
        e.length = 2
*/


二维数组

package com.chenny.Entity;

import java.util.Locale;

/****************** Exercise  *****************
 * 编写一个方法,能够产生二维精度型数据并加以初始化
 * 数据的容量由方法的形式参数决定,其初值必须落在另
 * 外两个形式参数所指定的区间之内。
 *编写第二个方法时,打印出第一个方法所产生的数组。在
 * main()中通过产生不同容量的数组并打印其内容来测试
 * 这两个方法
 ***********************************************/


public class TwoDoubleArray {
    private static void print(String s) {
        System.out.println(s);
    }
    private static void print() {
        System.out.println();
    }
    public static double[][] twoDDoubleArray(int xLen, int yLen, double valStart, double valEnd) {
        double[][] array = new double[xLen][yLen];
        double increment = (valEnd - valStart)/(xLen * yLen);
        double val = valStart;
        for(int i = 0; i < array.length; i++)
            for(int j = 0; j < array[i].length; j++) {
                array[i][j] = val;
                val += increment;
            }
        return array;
    }

    public static void printArray(double[][] array) {
        for(int i = 0; i < array.length; i++) {
            for(int j = 0; j < array[i].length; j++)
                System.out.printf(Locale.US, "%.2f ", array[i][j]);
            print();
        }
    }
    public static void main(String args[]) {
        double[][] twoD = twoDDoubleArray(4, 6, 47.0, 99.0);
        printArray(twoD);
        print("**********************");
        double[][] twoD2 = twoDDoubleArray(2, 2, 47.0, 99.0);
        printArray(twoD2);
        print("**********************");
        double[][] twoD3 = twoDDoubleArray(9, 5, 47.0, 99.0);
        printArray(twoD3);
    }
}


/*结果如下:

47.00 49.17 51.33 53.50 55.67 57.83 
60.00 62.17 64.33 66.50 68.67 70.83 
73.00 75.17 77.33 79.50 81.67 83.83 
86.00 88.17 90.33 92.50 94.67 96.83 
**********************
47.00 60.00 
73.00 86.00 
**********************
47.00 48.16 49.31 50.47 51.62 
52.78 53.93 55.09 56.24 57.40 
58.56 59.71 60.87 62.02 63.18 
64.33 65.49 66.64 67.80 68.96 
70.11 71.27 72.42 73.58 74.73 
75.89 77.04 78.20 79.36 80.51 
81.67 82.82 83.98 85.13 86.29 
87.44 88.60 89.76 90.91 92.07 
93.22 94.38 95.53 96.69 97.84 
*/


Arrays数组的实用功能


返回数组目录

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值