Java array learning experience, welcome brick

xiaoxiao2021-03-06  27

I used to be familiar with C , so I want to draw an array in C to the number in Java. After reading "Tij", I found that it is not such a thing, so I should refer to the arrays in Java. This article is not a comprehensive introduction to the number of Java in the Java, just some of my experience. (Some content switched from "Tij")

An Introduction: An array in Java has improved in Java relative to the arrays in C . First, the arrays in Java are objects, which means that it is different from the assembly of the arrays in C . Instead, the arrays in Java is more similar to the container classes in the STL or Java in C (just as objects, it Methods are much more than the container class or Collection class in STL). In addition, a set of algorithms that support arrays in Java are the same as static methods in the Algorithm in STL. Of course, strictly said that these algorithms may not emphasize the general algorithm that emphasizes the STL and Collections classes; however, they are an efficient algorithm containing in the standard library. For programmers, these algorithms can not be modified to each Different types of arrays are also an extent of a generic algorithm.

The following mainly discusses the characteristics of the Java array from the C array from the array as an object and the general algorithm.

1. Arch of Java as the benefits of objects 1.1 Crossing inspection

1.2 Length Field: Compared to arrays in traditional C , the Length field can be easily gadgetable size; but pay attention to, just get the size of the array, can not get how many elements in the array actually contain, because Length will only tell How many elements we can put into that array.

1.3 Initialization: The object array will automatically initialize NULL at the beginning of the creation, and the array composed of the original data type will automatically initial into zero (target type), (char) 0 (target type) or false (for Boolean).

1.4 array as return value: First, since the array is an object, then this object can be used as a return value; and if you don't have to worry if the array can be used automatically and the garbage collector will automatically Whose clearance

2. General Algorithm 2.1 ARRAYS array class in java.util holds a series of static methods to simplify our operation of array, with a total of four functions. Equals () is used to compare whether the two arrays are equal, and Fill () can fill a value in an array, and sort () can be sorted to the array, and binarysearch () is used to find an element in the arranging array. All of these methods have been used for all raw data types and objects. In addition, there is an AsList () method to use it to obtain any array and turn the array into a LIST container.

2.2 Sort and BinarySearch: There are two ways to provide comparison functions in Java 2. The first method is to use a natural comparative method, which is implemented by implementing a java.lang.comParable interface. The second approach provided by Java 2 for object comparisons, individually creates a class that implements a interface called Comparator. The interface provides two methods from Compare () and equals (). However, unless some special performance factors, we don't need to implement equals (), because it will inherit from Object by default each time you create a class, and Object already has an equals (). Comparator can be used as a parameter for the sort and binarysearch methods.

3. Item Need to note Problems 3.1 An array in Java can store both basic value types or storage. Object arrays and raw data type arrays are almost exactly consistent on the method of use. The only difference is that the object array is accommodated and the original data type array is accommodated for specific values. This should be paid to this attention to the discussion of questions about the array, must first determine the basic value type or object stored in the array. Especially when debugging, pay attention to this. For example: Arrays provides a Fill () method to copy a value to a location, if the object array is copied to each location. The Java Standard Library provides a static method named system.Arraycopy () to replicate the array for arrays. Copy an array is more than yourself write a for loop. Control all types. Regardless of the original data type array or an object array, we can copy them. But if you copy the object array, the true replication is just the reference object itself can be copied. 3.2 Why use array without using container classes such as ArrayList? Efficiency and type. 3.2.1 Efficiency: For Java, you want to save and random access a series of objects is actually the highest-efficiency method that the object reference efficiency is more than an array. 3.2.2 Type: The container class in the Java standard library treats objects as there is no specific type, in other words, they are treated as Object type. The Object Type is the root class of all classes in Java. It is very reasonable to see this method from a certain point of view. We only need to build a container and then all Java objects can enter that container. Except for the original data type, the Java base type package can be treated as a constant to the container or self-built one class as a variable value is treated as a variable value. This once again reflects that the array can make it to accommodate a specific type when creating an array than the superiority of the ordinary container. This means that the type of compilation time checks the type of time you set yourself or incorrectly extract a type instead of running Exception.

Summary: The first one is also the most effective choice when you want to accommodate a set of objects is an array.

转载请注明原文地址:https://www.9cbs.com/read-65153.html

New Post(0)