Java基础之数组

    数组和其他种类的容器之间的区别有三方面:效率、类型和保存基本类型的能力。在Java中,数组是一种效率最高的存储和随机访问对象引用序列的方式,它是一个简单的线性序列(数组和泛型能够转换)

package unit_sixteen;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

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();
        }
        System.out.println(Arrays.toString(spheres));
        System.out.println(spheres[4]);

        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));
        //数组长度是固定的,但是可以和集合泛型互换,数组剩下的就是效率!
        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));
    }
}
//一个类中只有一个public类
class BerylliumSphere {
    private static long counter;
    private final long id = counter++;

    public String toString() {
        return "Sphere" + id;
    }
}

     对象数组和基本类型数组在使用上几乎是相同的,唯一的区别就是对象数组保存的是引用,基本类型数组直接保存基本类型的值

package unit_sixteen;

import java.util.Arrays;

public class ArrayOptions {

    public static void main(String[] args) {
        BerylliumSphere[] a;
        BerylliumSphere[] b = new BerylliumSphere[5];
        System.out.println("b:" + Arrays.toString(b));
        BerylliumSphere[] c = new BerylliumSphere[4];
        for (int i = 0; i < c.length; i++) {
            if (c[i] == null) {
                c[i] = new BerylliumSphere();
            }
        }
        BerylliumSphere[] d = { new BerylliumSphere(), new BerylliumSphere(),
                new BerylliumSphere() };
        a = new BerylliumSphere[] { new BerylliumSphere(),
                new BerylliumSphere() };
        System.out.println("a.length=" + a.length);
        System.out.println("b.length=" + b.length);
        System.out.println("c.length=" + c.length);
        System.out.println("d.length=" + d.length);
        //将a指向了d的引用
        a = d;
        System.out.println("a.length=" + a.length);
        int[] e;
        int[] f = new int[5];
        System.out.println("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 };
        System.out.println("f.length=" + f.length);
        System.out.println("g.length=" + g.length);
        System.out.println("h.length=" + h.length);
        //将e指向了h的引用
        e = h;
        System.out.println("e.length=" + e.length);
        e = new int[] { 1, 2 };
        System.out.println("e.length=" + e.length);
    }
}

    sort(T[] a)和sort(T[] a,int fromIndex,int toIndex),其中sort(int[] a,int fromIndex,int toIndex)是对指定类型数组的指定范围按数字升序进行排序。排序的范围从索引 fromIndex包括)一直到索引 toIndex(不包括).如果 fromIndex==toIndex,则排序范围为空;binarySearch(T[] b,T key)和binarySearch(int[] a,int fromIndex,int toIndex,int key)其中binarySearch(T[] a,int fromIndex,int toIndex,int key)使用二分搜索法来搜索指定的 int 型数组的范围,以获得指定的值。必须在进行此调用之前对范围进行排序(通过 sort(int[], int, int) 方法)。如果没有对范围进行排序,则结果是不确定的。如果范围包含多个带有指定值的元素,则无法保证找到的是哪一个;equals(数据类型[],数据类型[])如果两个指定的数据类型数组彼此相等,则返回 true。如果两个数组包含相同数量的元素,并且两个数组中的所有相应元素对都是相等的,则认为这两个数组是相等的

package unit_sixteen;

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

public class Practice {

    public static void main(String[] args) {
        int[] a = new int[] { 11, 1, 2, 3, 4, 6, 4, 3, 5, 7 };
        int[] b = new int[] { 11, 1, 2, 3, 4, 6, 4, 3, 5, 7 };
        int[] c = new int[] { 5, 1, 2, 3, 4, 6, 4, 3, 5, 9 };
        System.out.println(Arrays.toString(a));
        // // 数组排序
        // Arrays.sort(a);
        // System.out.println(Arrays.toString(a));
        // Arrays.sort(b, 3, 8);
        // System.out.println(Arrays.toString(b));
        // // 调用binarySearch之前要对数组进行排序
        // Arrays.sort(a);
        // int m = Arrays.binarySearch(a, 11);
        // System.out.println(m);
        // Arrays.sort(b, 3, 8);
        // int n = Arrays.binarySearch(b, 3, 8, 6);
        // System.out.println(n);
        boolean blone = Arrays.equals(a, b);
        boolean bltwo = Arrays.equals(a, c);
        System.out.println(blone);
        System.out.println(bltwo);
    }

}
原文地址:https://www.cnblogs.com/yyxiangjava/p/5811363.html