数组的特殊之处
数组被创建之后,可以通过整型索引值访问他们的元素,数组的尺寸不能被更改
数组和其他容器的区别在于:效率,类型,和保存基本类型的能力。
数组一种效率最高的存储和随机访问对象引用序列的方式。
容器和数组的比较:
class BerylliumSphere{private static long counter;private final long id = counter++;@Overridepublic 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();}System.out.println(Arrays.toString(spheres));System.out.println(spheres[4]);List<BerylliumSphere> sphereList = new ArrayList<>();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<>(Arrays.asList(0,1,2,3,4,5));intList.add(97);System.out.println(intList);System.out.println(intList.get(4));}}
数组是第一级对象
数组的标识符只是一个引用,指向在堆中创建的一个真实的对象,数组对象保存指向其他对象的引用
length是数组对象的一部分,唯一一个可以访问的字段或方法,他表示的数组的大小而不是实际保存元素的个数
对象数组保存的是引用,基本类型的数组保存的是值
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;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 = {47, 85, 99};System.out.println("f.length: " + f.length);System.out.println("g.length: " + g.length);System.out.println("h.length: " + h.length);e = h;System.out.println("e.length: " + e.length);e = new int[]{1, 2};System.out.println("e.length: " + e.length);}}
返回一个数组
public class IceCream {private static Random rand = new Random(47);static final String[] FLAVORS = {"Chocolat", "Strawberry", "Fudge", "MiniChip"};public static String[] flavorsSet (int n) throws IllegalAccessException {if (n > FLAVORS.length){throw new IllegalAccessException("it's too big");}String[] res = new String[n];boolean[] picked = new boolean[FLAVORS.length];for (int i = 0; i < n; i++) {int t;do {t = rand.nextInt(FLAVORS.length);} while (picked[t]);res[i] = FLAVORS[t];picked[t] = true;}return res;}public static void main(String[] args) throws IllegalAccessException {for (int i = 0; i < 7; i++) {System.out.println(Arrays.toString(flavorsSet(3)));}}}
数组和泛型
可以创建非泛型的数组然后将其转型
public class ArrayOfGeneric {public static void main(String[] arg){List<String>[] ls;List[] la = new List[10];ls = (List<String>[]) la;Object[] objects = ls;objects[1] = new ArrayList<Integer>();List<BerylliumSphere>[] spheres = (List<BerylliumSphere>[]) new List[10];for (int i = 0; i < spheres.length; i++) {spheres[i] = new ArrayList<BerylliumSphere>();}}}
泛型在类或者方法的边界很有效,在类或者方法的内部,因为有擦除,所以泛型变得不适用
数组的实用功能
复制数组
System.arraycopy:
public class CopyingArrays {public static void main(String[] args) {int[] i = new int[7];int[] j = new int[10];Arrays.fill(i,47);Arrays.fill(j,99);System.out.println("i: " + Arrays.toString(i));System.out.println("j: " + Arrays.toString(j));System.arraycopy(i,0,j,0,i.length);System.out.println("j: " + Arrays.toString(j));int[] k = new int[5];Arrays.fill(k,103);System.out.println("k: " + Arrays.toString(k));//复制哪个数组,从哪里开始复制,粘贴到哪个数组,从哪里开始粘贴,粘贴的长度System.arraycopy(i,0,k,0,k.length);System.out.println("k: " + Arrays.toString(k));Arrays.fill(k,103);System.arraycopy(k,0,i,0,k.length);System.out.println("i: " + Arrays.toString(i));}}
数组的比较
equals:数组相等的条件是:元素个数必须相等,对应位置的元素也相等
public class ComparingArrays {public static void main(String[] args) {int[] a1 = new int[10];int[] a2 = new int[10];Arrays.fill(a1, 47);Arrays.fill(a2, 47);System.out.println(Arrays.equals(a1, a2));//实用的是Arrays.equals而不是a1.equals(a2),因为后者是Object的方法a2[3] = 11;System.out.println(Arrays.equals(a1,a2));String[] s1 = new String[4];Arrays.fill(s1,"Hi");String[] s2 = {new String("Hi"), new String("Hi"), new String("Hi"), new String("Hi")};System.out.println(Arrays.equals(s1, s2));}}
数组元素的比较
第一种实现Comparable接口,是自己的类拥有ComparaTo()的方法,如果当前对象小于参数,会返回0,如果大于参数会返回一个正值
第二种是Collections工具类中的reversrOrder()方法
