Java Array Initialization

Source: Internet
Author: User
Tags abs define arrays expression rand

Initializing an array in C is extremely error-prone and cumbersome. C + + makes it more secure by "set initialization" (annotation ⑥). Java does not have a "set" concept like C + +, because everything in Java is an object. But it does have its own array, which provides support through array initialization. An
array represents a series of objects or basic data types, all of which are encapsulated together-with a uniform identifier name. The definition and use of an array is done through the bracket indexing operator ([]). To define an array, simply follow the type name with a pair of empty square brackets:
int[] al;
You can also place the square brackets behind the identifier to achieve the exact same result:
int al[];
This format is consistent with the C and C + + Programmer's customary format. However, the most "fluent" may still be the previous syntax, because it indicates that the type is "an int array." This book will be in that format. The
compiler does not allow us to tell it how large an array is. This brings us back to the problem of "handle". At this point, all we have is a handle to the array, and no space has been allocated to the array. In order to create the appropriate storage space for an array, you must write an initialization expression. For arrays, initialization can occur anywhere in your code, but you can also use a special initialization expression that must appear where the array was created. This special initialization is a series of values enclosed by curly braces. The allocation of storage space (equivalent to using new) will be performed by the compiler in this case. For example:
int[] A1 = {1, 2, 3, 4, 5};
So why do you want to define an array handle that doesn't have an array?
Int[] A2;
in Java, in fact, you can assign an array to another, so you can use the following statement:
A2 = A1;
What we're really going to do is copy a handle, as shown here:

 

//: Arrays.java//Arrays of Primitives.
    public class Arrays {public static void main (string[] args) {int[] A1 = {1, 2, 3, 4, 5};
    Int[] A2;
    a2 = A1;
    for (int i = 0; i < a2.length; i++) a2[i]++;
  for (int i = 0; i < a1.length i++) prt ("a1[" + i + "] =" + a1[i]);
  } static void Prt (String s) {System.out.println (s); }
} ///:~


You see that A1 gets an initial value, and A2 does not; A2 will assign later-in this case to another array.
There is something new here: all arrays have an essential member (whether they are an object array or an array of primitive types) that can be queried-but not altered-to learn how many elements are contained within the array. This member is length. Similar to C and C + +, because Java arrays count from element 0, the maximum element number that can be indexed is "length-1". If the boundaries are exceeded, C and C + + will "silently" accept and allow us to use our own memory indiscriminately, which is the root of many bugs. However, Java preserves our vulnerability to this problem by generating a run-time error (a "violation", which is the subject of chapter 9th) once the boundary is exceeded. Of course, because of the need to check access to each array, it consumes a certain amount of time and extra code, and there's no way to turn it off. This means that group access can be an important reason for inefficient programs-if they are on a critical occasion. But given the security of Internet access and the programmer's programming efficiency, Java designers should think of it as worthwhile.
During program writing, what if you don't know how many elements are needed in your own array? At this point, simply create the element in the array with new. Here, even if you are going to create an array of basic data types, new will work as normal (new does not create a primitive type that is not an array):

 

: Arraynew.java
//creating arrays with new.
Import java.util.*;

public class Arraynew {
  static Random rand = new Random ();
  static int prand (int mod) {return
    Math.Abs (Rand.nextint ())% mod + 1;
  }
  public static void Main (string[] args) {
    int[] A;
    A = new Int[prand];
    PRT ("Length of a =" + a.length);
    for (int i = 0; i < a.length i++)
      prt ("a[" + i + "] =" + a[i]);
  }
  static void Prt (String s) {
    System.out.println (s);
  }
}///:~


Since the size of the array is randomly determined (using the Prand () method defined earlier), it is very obvious that the creation of the array is actually performed during the run time. In addition, from the output of this program, you can see that an array element of the base data type is automatically initialized to the "null" value (the null value is 0 for the value; for char, it is null; for Boolean, it is false).
Of course, the array may have been defined and initialized in the same statement, as follows:
Int[] A = new Int[prand (20)];
If you are manipulating an array of objects that are not of the base type, use new anyway. Here, we will encounter the handle problem again, because we are creating an array of handles. Please observe the wrapper type Integer, which is a class, not a basic data type:

//: Arrayclassobj.java//Creating an array of Non-primitive objects. Import java.util.*;
  public class Arrayclassobj {static Random rand = new Random ();
  static int prand (int mod) {return Math.Abs (Rand.nextint ())% mod + 1;
    public static void Main (string[] args) {integer[] a = new Integer[prand (20)];
    PRT ("Length of a =" + A.length);
      for (int i = 0; i < a.length i++) {A[i] = new Integer (Prand (500));
    PRT ("a[" + i + "] =" + a[i]);
  } static void Prt (String s) {System.out.println (s); }
} ///:~


Here, even after the new call begins to create the array:
Integer[] A = new Integer[prand (20)];
It is just an array of handles, and the initialization process does not end unless you initialize the object handle by creating a new integer object:
A[i] = new Integer (Prand (500));
If you forget to create an object, you get an "illegal" error when you try to read an empty array location at run time.
Let's look at the composition of the string object in the print statement. You can see that the handle to the integer object is automatically converted to produce a string that represents the value that is inside the object.
You can also use curly braces to close the list to initialize an array of objects. There are two forms, the first of which is the only form allowed by Java 1.0. The second (equivalent) Form of support is available from Java 1.1:

: Arrayinit.java
//Array initialization public

class Arrayinit {public
  static void Main (string[] args) { C5/>integer[] A = {
      new integer (1),
      new Integer (2),
      new Integer (3),
    };

    Java 1.1 only:
    integer[] b = new integer[] {
      new integer (1),
      new Integer (2),
      new Integer (3),
    };
  }
} ///:~


This is useful most of the time, but the limit is also greatest because the size of the array is determined during compilation. The last comma in the initialization list is optional (this feature makes maintenance of long lists easier).
The second form of array initialization (supported by Java 1.1) provides a simpler syntax for creating and invoking methods that get the same effect as C's "Variable argument list" (c usually referred to as "variable parameter table"). These effects include the number of unknown parameters (arguments) and the unknown type (if so selected). Since all classes are ultimately inherited from the universal root class object, you can create a method that takes an object array and calls it as follows:

: Varargs.java
//Using the Java 1.1 array syntax to create
//variable argument lists

class A {int i;} Public

class VarArgs {
  static void F (object[] x) {for
    (int i = 0; i < x.length; i++)
      System.out.printl N (x[i]);
  public static void Main (string[] args) {
    f (new object[] { 
        new Integer, New VarArgs (), 
        new Float (3.14), New Double (11.11)});
    F (New object[] {"One", "two", "three"});
    F (New object[] {new A (), new A (), New A ()});
  }
///:~


At this point, we can't take too much action on these unknown objects, and the program does something useful with each object using automatic string conversions. In the 11th chapter (runtime type Identification or RTTI), you will also learn how to investigate the exact types of objects so that you can do something interesting to them.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.