Java基础知识之数组

来源:互联网 发布:c 高斯算法 编辑:程序博客网 时间:2024/04/28 00:01

一:什么是数组:

数组是相同类型的,用一个标识符名称封装到一起的一个对象序列或基本数据类型序列。
数组就是一个简单的线性序列,这使得元素访问非常快速,但是为这种速度所付出的代价是数组对象的大小被固定,并且在其生命周期中不可改变。
在Java中数组是一种效率最高的存储和随机访问对象引用序列的方式。

二:数组和其它容器有什么区别:

1. 数组可以存储基本数据类型,也可以存储引用数据类型,集合只能存储引用数据类型。
数组可以持有基本数据类型,而容器不能持有基本数据类型,但是JAVA SE5 泛型出来后,容器就可以指定并检查它们所持有的对象类型,并且有了自动包装机制,“容器看起来还能够持有基本类型”(这里只是看起来,实际上是持有的基本类型多对应的包装器类型)。

2. 数组是固定长度的,集合的长度是可变的。
数组是一种内存结构,而容器是一种数据结构。数组创建就必须指定大小,指定了大小数组也就固定了。当数组空间不足的时候,只能创建一个新的数组,然以把数据拷贝到新数组中。
知道数组的长度,而且以后也不会再增加,那肯定就使用数组了;如果数组的长度不定或者说是长度会增加,为了方便起见使用容器

三:数组的创建与初始化:

public class ArrayTest {    public static void main(String[] args) {        int[] a = new int[5];                       //第一种初始化方法        System.out.println(Arrays.toString(a));     //[0, 0, 0, 0, 0]        int[] b = new int[]{1, 2, 3, 4, 5};         //第二种初始化方法        System.out.println(Arrays.toString(b));     //[1, 2, 3, 4, 5]        int[] c = {1, 2, 3, 4, 5};                  //第三种初始化方式        System.out.println(Arrays.toString(c));     //[1, 2, 3, 4, 5]    }}

四:多维数组:

对于一个二维数组来说:它是一个数组,它的每个元素都是一个一维数组。
对于n(n>1)维数组来说:它是一个数组,它的每个元素都是一个(n-1)维数组。

二维数组声明和初始化:

public class ArrayTest {    public static void main(String[] args) {        int[][] a = new int[5][3];                       //第一种初始化方法,表示有5个长度为3的一维数组        System.out.println(Arrays.deepToString(a));     //[[0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0], [0, 0, 0]]        System.out.println(a.length);                   //5        int[][] b = new int[][]{{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}};//第二种初始化方法,表示有2个长度为5的一维数组        System.out.println(Arrays.deepToString(b));     //[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]        System.out.println(b.length);                   //2        int[][] c = {{1, 2, 3, 4, 5}, {6, 7, 8, 9, 10}};//第三种初始化方式        System.out.println(Arrays.deepToString(c));     //[[1, 2, 3, 4, 5], [6, 7, 8, 9, 10]]        System.out.println(c.length);                   //2        int[][] d = new int[2][];                       //第四种创建方式,数组中构成矩阵的每个向量都可以具有任意长度,也就是说构成二维数组的每个元素的一维数组的长度可以任意长度。        for (int i = 0; i < d.length; i++) {            d[i] = new int[i + 1];        }        System.out.println(Arrays.deepToString(d));     //[[0], [0, 0]]        System.out.println(d.length);                   //2    }}

五:Arrays实用功能:

  • Arrays.fill():填充数组
    用同一个值来填充数组的各个位置,而针对对象而言,就是复制同一个引用进行填充。
public class ArrayTest {    public static void main(String[] args) {        int[] a = new int[5];                       //创建数组,默认值为0        System.out.println(Arrays.toString(a));     //[0, 0, 0, 0, 0]        Arrays.fill(a, 6);                           //将数据全部替换成6        System.out.println(Arrays.toString(a));     //[6, 6, 6, 6, 6]        boolean[] b = new boolean[6];                 //创建数组,默认值为false        System.out.println(Arrays.toString(b));     //[false, false, false, false, false, false]        Arrays.fill(b, true);                        //将数据全部替换成true        System.out.println(Arrays.toString(b));     //[true, true, true, true, true, true]        Animal[] c = new Animal[7];                 //创建数组,默认值为null        Animal animal = new Animal("animal");       //创建将要替换的对象        System.out.println(Arrays.toString(c));     //[null, null, null, null, null, null, null]        Arrays.fill(c, animal);                     //将数据全部替换成animal        System.out.println(Arrays.toString(c));     //[com.lonbon.mytest.Animal@1540e19d, com.lonbon.mytest.Animal@1540e19d, com.lonbon.mytest.Animal@1540e19d, com.lonbon.mytest.Animal@1540e19d, com.lonbon.mytest.Animal@1540e19d, com.lonbon.mytest.Animal@1540e19d, com.lonbon.mytest.Animal@1540e19d]    }}
  • System.arraycopy():复制数组
    Java标准类库提供有static方法System.arraycopy(),用它复制数组比用for循环复制要快很多。
    我们来看一下这个方法:public static void arraycopy(Object src, int srcPos, Object dest, int destPos, int length)
    src:原数组
    srcPos:原数组起始位置
    dest:目标数组
    destPos:目标数组的起始位置
    length:要复制的数组元素的数目
    下面看一个简单例子:
public class ArrayTest {    public static void main(String[] args) {        int[] a = new int[5];        Arrays.fill(a, 6);        int[] b = new int[7];        System.arraycopy(a, 0, b, 0, a.length);        System.out.println(Arrays.toString(b));     //[6, 6, 6, 6, 6, 0, 0]    }}
  • Arrays.equals():数组的比较
    Arrays类提供了重载后的equals()方法,用来比较整个数组。数组相等的条件是元素个数必须相等并且对应的元素也相等。
    看下面例子:
public class ArrayTest {    public static void main(String[] args) {        int[] a = new int[10];        int[] b = new int[10];        Arrays.fill(a, 6);        Arrays.fill(b, 6);        System.out.println(Arrays.equals(a, b));     //true        String[] c = new String[10];        String[] d = new String[10];        Arrays.fill(c, "q");        Arrays.fill(d, "w");        System.out.println(Arrays.equals(c, d));     //false    }}
  • java.lang.Comparable:数组对象的排序:
    我们只需要实现Comparable接口,并且在compareTo()里面定义自己的排序规则就可以实现自定义的对象排列。如果当前对象小于参数则返回负值,如果相等则返回零,如果当前对象大于参数则返回正值。
    下面我们来看一个例子
public class ArrayTest implements Comparable<ArrayTest> {    private int i;    public ArrayTest(int i) {        this.i = i;    }    @Override    public int compareTo(@NonNull ArrayTest another) {                  //自定义排序规则        return (i < another.i ? -1 : (i == another.i ? 0 : 1));    }    @Override    public String toString() {        return i+"";    }    public static void main(String[] args) {        ArrayTest[] a = {new ArrayTest(10), new ArrayTest(30), new ArrayTest(3), new ArrayTest(7), new ArrayTest(1)};        System.out.println(Arrays.toString(a));     //[10, 30, 3, 7, 1]          Arrays.sort(a);        System.out.println(Arrays.toString(a));     //[30, 10, 7, 3, 1]    }}