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.
posted @ 2016-12-19 19:34  玄天强  阅读(374)  评论(0编辑  收藏  举报