DAY09--数组

数组

为什么需要数组?

  • 持有大量对象

数组的优势

  • 效率:存储效率最高
  • 类型:
  • 保存基本类型的能力

数组的缺点

  • 长度固定,不可动态增长,需要动态增长只能改用容器类型

使用规则

  • 通过下标访问元素(是唯一方式)
  • 下标越界会报RuntimeException
public class BerylliumSphere {
    private static int counter;
    private int id=counter++;
    @Override
    public String toString() {
        return "BerylliumSphere "+id;
    }

    public static void main(String[] args) {
        BerylliumSphere[] spheres=new BerylliumSphere[10];
        for (int i = 0; i <5 ; i++) {
            spheres[i] = new BerylliumSphere();
        }
        System.out.println(Arrays.toString(spheres));

        int[] integers = {0, 1, 2, 3, 4, 5};
        System.out.println(Arrays.toString(integers));

        LinkedList<BerylliumSphere> spheres1 = new LinkedList<>();
        for (int i = 0; i <5 ; i++) {
            spheres1.add(new BerylliumSphere());
        }
        System.out.println(spheres1);
        spheres1.add(new BerylliumSphere());
        System.out.println(spheres1);
    }
}
//output
[BerylliumSphere 0, BerylliumSphere 1, BerylliumSphere 2, BerylliumSphere 3, BerylliumSphere 4, null, null, null, null, null]
[0, 1, 2, 3, 4, 5]
[BerylliumSphere 5, BerylliumSphere 6, BerylliumSphere 7, BerylliumSphere 8, BerylliumSphere 9]
[BerylliumSphere 5, BerylliumSphere 6, BerylliumSphere 7, BerylliumSphere 8, BerylliumSphere 9, BerylliumSphere 10]
数组是第一级对象

数组对象里面包含着对其他对象的引用

数组对象的创建方式

  • 隐式创建:int[] arr={1,2,3}
  • 显示创建:int[] arr=new int[]{1,2,3}

两种数组:普通数组保存的是基本类型的值,对象数组保存的是对象的引用

返回一个数组

java数组的好处:不用去管数组的生命周期

public class IceCream {
    private static Random random = new Random(47);
    static String[] FLAVORS = {
            "chocolate", "Apple", "Vanilla", "Banana"
    };

    public static String[] flavorSet(int size) {
        String[] result = new String[size];
        //不能用Boolean,对象数组元素的默认值就是null了
        boolean[] flag = new boolean[FLAVORS.length];
        for (int j = 0; j <size ; j++) {
            int r;
            do {
                r = random.nextInt(FLAVORS.length);//目的是选出个随机的,不与之前结果重复的r
            } while (flag[r]);
            result[j] = FLAVORS[r];
            flag[r]=true;
        }
        return result;
    }
    public static void main(String[] args) {
        // [Vanilla, Apple, chocolate]
        System.out.println(Arrays.toString(flavorSet(3)));
    }
}
多维数组

用{}分隔,多维数组中构成矩阵的向量可以是任意长度,(把频谱竖起来看就是)

数组与泛型

数组与泛型不能很好的结合,不能实例化参数化类型的数组,但是你可以创建具有参数化类型数组的引用

Arrays

Arrays类里面有一组实用的static方法

  • equals,deepEquals(适用与多维数组)方法,里面是调用每个元素的equals方法来比较的,只要你元素重写了equals方法就行,基本类型和String类型则是直接比较值
  • fill:填充数组,结果是一个数组都是同一个值
  • sort:用于数组排序,sort方法还重载了有比较器Comparator的方法,这样就允许按照你的比较规则来排序
  • binarySearch:用于已排序的数组中查找元素,必须是已排序的数组
  • toString:生成数组的String表示
  • asList:接收任意序列或者是数组作为参数,生成List

System.arrayCopy:基本类型和引用类型的数组都可以拷贝,是浅拷贝(即只复制了引用,没有复制对象)

问题

什么是编译期检查?

java中数组arr.length是怎样实现的?

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值