zoukankan      html  css  js  c++  java
  • thinking in java笔记 16 数组

    ***数组的特点
       数组与其他容器的区别:效率、类型和保存基本类型的能力。java中数组是一种效率最高的存储和随机访问对象引用序列的方式。数组就是一个简单的线性序列,这使得元素访问非常快速。但其代价是数组对象的大小被固定,并且在其生命周期不可被改变。
       数组之所有优于泛型之前的容器,因为:
       1 可以创建一个数组去持有某个类型,通过编译器检查来防止插入的错误类型和抽取不当类型。
         2 数组可以持有基本类型
       数组和泛型之后的容器都是类型检查的,差异在与数组使用[]访问元素,而容器使用get set add等方法。两者十分相似,切换容易,容器具有更多的功能。
       自动包装机制出现后,容器可以与数组一样方便的处理基本类型,数组唯一的优点是效率,但解决一般化的问题,使用更多的是容器。

    ***数组是第一级对象
       数组标识符只是一个引用,指向在堆中创建的一个真实对象,这个对象用以保存指向其他对象的引用。可以作为数组初始化语法的一部分隐式地创建此对象,或用new显式创建。
       对象数组保存的是引用,基本类型数组保存的是基本类型的值。

    ***多维数组
       数组中构成矩阵的每个向量都可以具有任意的长度(粗糙数组)。

    ***Arrays类
           fill                       填充数组
          sort                      对数组排序
            Arrays.sort(Comparable a,Comparator b)
            a为实现了Comparable  接口的类,此接口只需要重写compareto方法。
            b为实现了Comparator接口的类,为排序模型  ,基本类型数组无法使用 Comparator进行排序。
            对没有重复元素的数组排序,可以使用TreeSet(保持排序顺序),LinkedHashSet(保持插入顺序)
          toString    产生数组的String表示
              deepToString          打印多维数组
          hashCode   产生数组的散列码
          asList      接受任意的序列或数组作为参数,并转变为List容器
          equals     比较数组是否相等
          deepEquals    多维数组比较
          binarySearch    在已排序的数组中执行快速查找。如果排序时使用了 Comparator,则必须用同样的 Comparator进行binarySearch。     

    ***System.arrayCopy
       数组copy方法。不会执行自动包装和拆包,两个数组必须类型相同。基本类型copy的是值,对象数组copy的是引用,称为浅复制(shallow copy).

    ***数组与泛型
       不能实例化具有参数化类型的数组,但可以参数化数组本身的类型,或创建非泛型的数组,然后将其转型:
       1 
         
    class ClassParameter<T> {
    public T[] f(T[] arg) {
    return arg;
    }
    }

    class MethodParameter {
    public static <T> T[] f(T[] arg) {
    return arg;
    }
    }

    public class ParameterizedArrayType {
    public static void main(String[] args) {
    Integer[] ints = {
    1, 2, 3, 4, 5
    };
    Double[] doubles = {
    1.1, 2.2, 3.3, 4.4, 5.5
    };
    Integer[] ints2 = new ClassParameter<Integer>().f(ints);
    Double[] doubles2 = new ClassParameter<Double>().f(doubles);
    ints2 = MethodParameter.f(ints);
    doubles2 = MethodParameter.f(doubles);
    }
    }

       2 
        List<String>[] ls;
    List[] la = new List[10];
    ls = (List<String>[])la; // "Unchecked" warning
    ls[0] = new ArrayList<String>();

       泛型在类或方法的边界处很有效,而在类或方法的内部,擦除通常会使泛型变得不适用,如,不能创建泛型数组。
         public class ArrayOfGenericType<T> {
    T[] array; // OK
    @SuppressWarnings("unchecked")
    public ArrayOfGenericType( int size) {
    //! array = new T[size]; // Illegal
    array = (T[]) new Object[size]; // "unchecked" Warning
    }
    // Illegal:
    //! public <U> U[] makeArray() { return new U[10]; }
    } ///:~



  • 相关阅读:
    ExecuteScalar requires the command to have a transaction when the connection assigned to the command is in a pending
    如何从vss中分离程序
    String or binary data would be truncated
    the pop3 service failed to retrieve authentication type and cannot continue
    The POP3 service failed to start because
    IIS Error he system cannot find the file specified _找不到页面
    pku2575Jolly Jumpers
    pku2940Wine Trading in Gergovia
    pku3219二项式系数
    pku1029false coin
  • 原文地址:https://www.cnblogs.com/myparamita/p/2203989.html
Copyright © 2011-2022 走看看