`
leonzhx
  • 浏览: 793451 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

Chapter 16. Arrays -- Thinking in Java

阅读更多

1) There are three issues that distinguish arrays from other types of containers: efficiency, type, and the ability to hold primitives. The array is Java’s most efficient way to store and randomly access a sequence of object references. Arrays are superior to pre-generic containers because you create an array to hold a specific type. This means that you get compile-time type checking to prevent you from inserting the wrong type or mistaking the type that you’re extracting.

 

2) 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, and it can be created either implicitly, as part of the array initialization syntax, or explicitly with a new expression. Part of the array object (in fact, the only field or method you can access) is the read-only length member that tells you how many elements can be stored in that array object. The '[ ]' syntax is the only other access that you have to the array object.

Commented by Sean: another mehtod of array is clone which is overrided specifically for array.

 

3) when an array object is created, its references are automatically initialized to null. Similarly, an array of primitives is automatically initialized to zero for numeric types, (char)0 for char, and false for boolean.

 

4) The "aggregate initialization" syntax (i.e. {1,2,3} ) that causes the array object to be created (implicitly with new on the heap). The aggregate initialization must be used at the point of array definition but with the "dynamic aggregate initialization"(i.e. new int[]{1,2,3}) you can create and initialize an array object anywhere.

 

5) Java SE5 Arrays.deepToString( ) method turns multidimensional arrays into Strings. When you initialize multi-dimensional arrays , you can delimit each vector in the array by using curly braces.

 

6) Primitive array values are automatically initialized to zero if you don’t give them an explicit initialization value. Arrays of objects are initialized to null.

 

7) Each vector in the arrays that make up the matrix can be of any length, which is called a ragged array.

 

8) You cannot instantiate arrays of parameterized types. ( i.e. new HashMap<String>[10])You cannot create an array of a generic type. ( i.e. new T[10])

 

9) Although you cannot create an actual array object that holds generics, you can create an array of the non-generified type and cast it.

 

10) The Java standard library Arrays class has a rather trivial fill( ) method: It only duplicates a single value into each location, or in the case of objects, copies the same reference into each location.

 

11) All Collection subtypes have a toArray( ) method that will fill the argument array with the elements from the Collection.

 

12) In java.util, you’ll find the Arrays class, which holds a set of static utility methods for arrays. There are six basic methods: equals( ), to compare two arrays for equality (and a deepEquals( ) for multidimensional arrays); fill( ), to fill each element of an array with single value; sort( ), to sort an array; binarySearch( ), to find an element in a sorted array; toString( ), to produce a String representation for an array; and hashCode( ), to produce the hash value of an array. All of these methods are overloaded for all the primitive types and Objects. In addition, Arrays.asList( ) takes any sequence or array and turns it into a List container.

 

13) The Java standard library provides a static method, System.arraycopy( ), which can copy arrays far more quickly than if you use a for loop to perform the copy by hand. The arguments to arraycopy( ) are the source array, the offset into the source array from where to start copying, the destination array, the offset into the destination array where the copying begins, and the number of elements to copy. Naturally, any violation of the array boundaries will cause an exception.

 

14) 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.

 

15) Java has two ways to provide comparison functionality:

    a. The first is with the "natural" comparison method that is imparted to a class by implementing the java.lang.Comparable interface. This is a very simple interface with a single method, compareTo( ). This method takes another object of the same type as an argument and produces a negative value if the current object is less than the argument, zero if the argument is equal, and a positive value if the current object is greater than the argument. If Comparable hadn’t been implemented, then you’d get a ClassCastException at run time when you tried to call sort( ). This is because sort( ) casts its argument to Comparable.

    b. You create a separate class that implements an interface called Comparator. This is an example of the Strategy design pattern. It has two methods, compare( ) and equals( ). However, you don’t have to implement equals( ) except for special performance needs, because anytime you create a class, it is implicitly inherited from Object, which has an equals( ). So you can just use the default Object equals( ) and satisfy the contract imposed by the interface. The Collections class contains a method reverseOrder( ) that produces a Comparator to reverse the natural sorting order.

 

16) 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.

 

17) Arrays.binarySearch( ) produces a value greater than or equal to zero if the search item is found. Otherwise, it produces a negative value representing the place that the element should be inserted if you are maintaining the sorted array by hand. The value produced is : -(insertion point) - 1
The insertion point is the index of the first element greater than the key, or array.length, if all elements in the array are less than the specified key. If an array contains duplicate elements, there is no guarantee which of those duplicates will be found. If you sort an object array using a Comparator (primitive arrays do not allow sorting with a Comparator), you must include that same Comparator when you perform a binarySearch( ).

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics