There are many ways to save objects. What are the special features of arrays? There are two main differences between arrays and other containers: efficiency and type. First, efficiency: in Java, arrays are the most efficient way to store and randomly access the reference sequence of objects. ** An array is a simple linear sequence. However, when an array object is created (the array is regarded as an object), the size of the array is fixed, and the lifecycle of the array cannot be changed. Generally, an array of a specific size is created. When the space is insufficient, a new array is created, and all references in the old array are moved to the new array. In C ++, the vector of the container class does know the type of the object saved by itself, but it has one drawback: The vector operator [] in C ++ does not perform the boundary check, therefore, operations may be performed out of the border. In Java, both arrays and containers have boundary checks. If the operation is out of bounds, A runtimeexception is returned. Runtimeexception is usually a programmer's error, so you do not have to perform cross-border checks on your own. To speed up, the C ++ vector does not perform a boundary check on each access. Java arrays and containers impose fixed performance overhead due to the existing boundary check. Second, type: Only Arrays can store basic types. The list, set, and map containers do not process objects of specific types. In other words, they process all objects according to the object type. From a certain point of view, this method is good: you only need to build a container, any Java object can be put into it. Arrays can be placed in containers as constants for using the Java basic packaging class, or as variable values packaged in your own class. This is the second advantage of arrays over general containers: When you create an array, it can only save specific types (arrays can save basic types, but containers cannot ). This means that the type check will be performed during compilation to prevent the wrong type from being inserted into the array or the wrong type when the data is retrieved. Of course, Java will prevent you from sending inappropriate messages to objects at both compilation and runtime. Therefore, it is not to say which method is less secure, but if errors can be identified during compilation, the program can run faster and the possibility of program users being frightened by exceptions is reduced. Considering the efficiency and type check, array should be used as much as possible. However, to solve the more general problem, the array may be too limited.
Array is the first level object no matter which type of array is used, the array identifier is actually a reference, pointing to a real object created in the heap, this (array) objects are used to save references to other objects. Read-Only member length is a part of an array object. In fact, this is the only field and method in the array that can be accessed. The "[]" syntax is the only way to access an array object. The following example describes how to initialize an array and how to assign a value to the array reference so that it points to another array object. This example also shows that ** the object array and the basic type array are almost the same. The only difference is that the object array stores references, the basic type array directly saves the value of the basic type.
/** * Title: * Description: * Company: Augmentum Inc<br> * Copyright: 2008 (c) Thinking in Java<br> * @author: Forest He * @version: 1.0 */package com.augmentum.foresthe;class Weeble {} // A samll mythical creaturepublic class ArraySize {public static void main(String[] args) {/** * Arrays of objects */Weeble[] a;Weeble[] b = new Weeble[5];Weeble[] c = new Weeble[4];Weeble[] d = { new Weeble(), new Weeble(), new Weeble() };a = new Weeble[] { new Weeble(), new Weeble() };for (int i = 0; i < c.length; i++) if (c[i] == null) c[i] = new Weeble();System.out.println("a.length = " + a.length);System.out.println("b.length = " + b.length);System.out.println("c.length = " + c.length);System.out.println("d.length = " + d.length);a = d;System.out.println("a.length = " + a.length);/** * Arrays of primitives */int[] e;int[] f = new int[5];int[] g = new int[4];int[] h = { 11, 47, 93 };System.out.println("f.length = " + f.length);System.out.println("g.length = " + g.length);System.out.println("h.length = " + h.length);e = h;System.out.println("e.length = " + e.length);e = new int[] { 1, 2 };System.out.println("e.length = " + e.length);}}
Array A is a local variable that has not yet been initialized. Before you initialize it correctly, the compiler does not allow this reference to do anything. ** Array B is initialized to point to an array referenced by Weeble, but no Weeble object is actually placed in the array. However, you can still access the array size because B points to a legal object. There is a small drawback: you cannot know the exact number of elements in the array, because length only indicates how many elements the array can accommodate. That is to say, length is the size of the array, rather than the number of actually stored elements. When an array object is generated, all references are automatically initialized to null. Therefore, check whether the reference is null to check whether an object exists at a certain position in the array. Array C indicates that after an array object is created, all positions of the array are assigned a Weeble object. ** Array d Indicates creating an array object using the "aggregation initialization" syntax (implicitly using new to create in the heap, just like array C ), the initialization process of a Weeble object uses only one statement. The clustering initialization operation of array D must be used at the position defined by D. However, if the following syntax is used: "dynamic clustering initialization", you can create and initialize array objects at any location. For example, assume that the hide () method requires an array of Weeble objects as input parameters. You can call hide (d) in this way, but you can also dynamically create an array to be passed as a parameter: Hide (New Weeble [] {New Weeble (), new Weeble ()}); in many cases, this syntax makes code writing more convenient. Expression: A = D; specifies how to assign a reference to an array object to another array object, which is no different from other types of object references. PS: The container class can only save object references. The array can directly Save the basic type, and can also save the object reference. You can use the "Wrap" Class in the container, such as integer and double, to put the value of the basic type into the container. However, compared with the basic type, the package class is clumsy. In addition, it is more efficient to create and access an array of the basic types than to package the basic types of containers.
Returns an array C and C ++. Instead of an array, returns a pointer to an array. However, Java allows direct return of an array. In addition, returning an array is no different from returning any other object (actually returning a reference.
/*** Title: * Description: * company: Augmentum Inc <br> * copyright: 2008 (c) Thinking in Java <br> * @ Author: Forest he * @ version: 1.0 */package COM. augmentum. foresthe; import Java. util. random; public class icecream {Private Static random Rand = new random (); public static final string [] flavors = {"Chocolate", "strawberry", "Vanilla Fudge Swirl ", "Mint chip", "mocha almond fudge", "rum raisin", "Praline cream", "mud pie"};/*** the returned array capacity is n, determined by the parameters of the Input Method */public static string [] flavorset (int n) {string [] Results = new string [N]; Boolean [] picked = new Boolean [flavors. length]; for (INT I = 0; I <flavors. length; I ++) Picked [I] = false; For (INT I = 0; I <n; I ++) {int T; /*** ensure that duplicate random numbers are not generated */DOT = Rand. nextint (flavors. length); While (picked [T]); Results [I] = flavors [T]; picked [T] = true;} return results ;} public static void main (string [] ARGs) {for (INT I = 0; I <20; I ++) {system. out. println ("flavorset (" + I + ") ="); string [] F1 = flavorset (flavors. length); For (Int J = 0; j <f1.length; j ++) system. out. println ("/t" + F1 [J]) ;}}