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.
  • 相关阅读:
    数据结构HashMap(Android SparseArray 和ArrayMap)
    一篇文章教你读懂UI绘制流程
    死磕安卓前序:MVP架构探究之旅—基础篇
    我就死磕安卓了,怎么了?
    戏说移动江湖开发历程
    姿势摆好,一招学会android的布局优化!
    学习React Native必看的几个开源项目
    开发了几个小程序后,说说我对小程序的看法
    jQuery基础一
    JavaScript基础二
  • 原文地址:https://www.cnblogs.com/apolloqq/p/6198834.html
Copyright © 2011-2022 走看看