zoukankan      html  css  js  c++  java
  • Thinking in Java——笔记(16)

    Arrays


    Why arrays are special

    • There are three issues that distinguish arrays from other types of containers: efficiency, type, and the ability to hold primitives.
    • The cost of this speed is that the size of an array object is fixed and cannot be changed for the lifetime of that array.
    • You should generally prefer an ArrayList to an array.
    • You’ll get a RuntimeException if you exceed the bounds, indicating a programmer error.
    • Arrays are superior to pre-generic containers because you create an array to hold a specific type.

    Arrays are first-class objects

    • The array identifier is actually a reference to a true object that’s created on the heap. This is the object that holds the references to the other objects.
    • length member that tells you how many elements can be stored in that array object.

    Returning an array

    • Returning an array is just like returning any other object—it’s a reference.
    • The garbage collector takes care of cleaning up the array when you’re done with it, and the array will persist for as long as you need it.

    Multidimensional arrays

    • Each vector in the arrays that make up the matrix can be of any length.
    • The Arrays.deepToString( ) method works with both primitive arrays and object arrays.

    Arrays and generics

    • You cannot instantiate arrays of parameterized types.
    • Erasure removes the parameter type information, and arrays must know the exact type that they hold, in order to enforce type safety.
    • You can parameterize the type of the array itself.
    • The compiler won’t let you instantiate an array of a generic type. However, it will let you create a reference to such an array.
    • Although you cannot create an actual array object that holds generics, you can create an array of the non-generified type and cast it.
    • A generic container will virtually always be a better choice than an array of generics.

    Creating test data

    Arrays.fill()

    • Since you can only call Arrays.fill( ) with a single data value, the results are not especially useful.

    Data Generators

    • If a tool uses a Generator, you can produce any kind of data via your choice of Generator.

    Array Utilities

    • There are six basic methods in Arrays: equals(), fill(), binarySearch(), sort(), toString(), hashCode().

    Copying an array

    • The Java standard library provides a static method, System.arraycopy( ), which can copy arrays.
    • If you copy arrays of objects, then only the references get copied—there’s no duplication of the objects themselves.

    Comparing arrays

    • To be equal, the arrays must have the same number of elements, and each element must be equivalent to each corresponding element in the other array, using the equals( ) for each element.

    Array element comparisons

    • You hand a Strategy object to the code that’s always the same, which uses the Strategy to fulfill its algorithm.
    • sort( ) casts its argument to Comparable.

    Sorting an array

    • The sorting algorithm that’s used in the Java standard library is designed to be optimal for the particular type you’re sorting—a Quicksort for primitives, and a stable merge sort for objects.

    Searching a sorted array

    • If you try to use binarySearchC ) on an unsorted array the results will be unpredictable.
    • Otherwise, it produces a negative value representing the place that the element should be inserted if you are maintaining the sorted array by hand.
    • If an array contains duplicate elements, there is no guarantee which of those duplicates will be found.
  • 相关阅读:
    “指定的SAS安装数据(sid)文件不能用于选定的SAS软件订单
    windows下如何快速优雅的使用python的科学计算库?
    量化分析师的Python日记【第1天:谁来给我讲讲Python?】
    Python的lambda函数与排序
    使用python管理Cisco设备-乾颐堂
    python移除系统多余大文件-乾颐堂
    python算法
    python实现高效率的排列组合算法-乾颐堂
    使用python把图片存入数据库-乾颐堂
    Python将阿拉伯数字转化为中文大写-乾颐堂
  • 原文地址:https://www.cnblogs.com/apolloqq/p/6198834.html
Copyright © 2011-2022 走看看