Java array of features and the use of basic skills

Author:Anonymous    Updated:2008-2-26 22:21:30
1. On the characteristics of an array
1) in Java, or whether to use an array of containers, both border checkpoints. If the cross-border operation will be a RuntimeException anomaly.

2) preservation of the array can only specific types. Array can be saved basic types, containers are not. Vessels not to deal with specific types of objects, and they will all objects are handled by Object type.

3) Items can only preserve object. The array can be stored directly for the creation of basic types, but also can be saved object. In containers can be used in packaging, such as Integer, Double to achieve, such as the preservation of the basic data types.

4) Object array and an array of basic types in the use of almost the same; The only difference is that the object is the preservation of the array used an array of basic types preserve basic types of value.

2. On the definition of an array

1) array in the definition, can not be allocated space. Only the latter definition can be assigned to an array of space.

Int num [];

Num = new int [3];

Or

Int num = new int [] [3];

Attention

Int [] num = new int [] (1,2,3) / / ok

Int [] num = new int [3] (1,2,3) / / error;

2) It can be defined two-dimensional array.

Int [] [] num;

/ / Or

Num = new int [] [3];

Num [0] = new int [5];

Num [1] = new int [3];

Num [2] = new int [2];

3) Fu initial two-dimensional array.


Int [] [] = new int num [][]{ 1,2,3,4,5,5); / / error
Int [] [] = new int num [][]{{ 1,2,3), (4,5,5)) / / ok
Int [] [] num = new int [2 ][]{{ 1,2,3), (4,5,5)) / / error
Int num [] [] = ((1,2,3), (4,5,6)) / / ok
 

3. On the array initialization

Object in the creation of the beginning of the array will be automatically initialized as null, from a primitive data type will automatically initialize the array into zero (for numerical types), (Char) 0 (for the character type) or false (for the Boolean type) .

4. The array of problems cited

Int a1 [] = (1, 2, 3, 4, 5);

Int [] a2;

A2 = a1; / / Here is a copy of the quote

See the following code:


(Public class Arrays
     Public static void main (String [] args) (
        Int a1 [] = (1, 2, 3, 4, 5);
        For (int i = 0; i <a1.length; i + +)
            System.out.println ( "a1 [" + i + "] =" [i] + a1);
        Int [] a2;
        A2 = a1;
        For (int i = 0; i <a2.length; i + +)
            A2 [i] + +;
        System.out.println ("----- after change a2 ------ ");
        For (int i = 0; i <a1.length; i + +)
            System.out.println ( "a1 [" + i + "] =" [i] + a1);
        System.out.println ("----- after change a2 [0 ]------");
        A2 [0] = 333;
        System.out.println ( "a2 [0] =" + a2 [0]);
        System.out.println ( "a1 [0] =" + a1 [0]);
        System.out.println ("----- a2 ------ ");
        For (int i = 0; i <a2.length; i + +)
            System.out.println ( "a2 [" + i + "] =" [i] + a2);
     )
)
 

Results:

A1 [0] = 1

A1 [1] = 2

A1 = 3 [2]

A1 [3] = 4

A1 = 5 [4]

------ ----- After change a2

A1 [0] = 2

A1 [1] = 3

A1 [2] = 4

A1 [3] = 5

A1 = 6 [4]

----- After change a2 [0 ]------

A2 [0] = 333

A1 [0] = 333

------ ----- A2

A2 [0] = 333

A2 [1] = 3

A2 [2] = 4

A2 [3] = 5

A2 = 6 [4]

A1 and a2 array always in operation with an object.

5. On the array of related operation

1) In Java, all of the array has a default attribute length for access to the number of elements in an array.

2) an array of reproduction: System.arraycopy ().

3) array sort: Arrays.sort ().

4) has been sort of an element of the array View: Arrays.binarySearch ().

6. On the array of sort operations

1) Object array sort, we must realize Comparable interface.


Import java.util.Arrays;
Class Student implements Comparable (
     Int num;
     String name;
 
     Student (int num, String name) (
        This.num = num;
        This.name = name;
     )
 
     Public String toString () / / rewrite toString () method, in order to main: System.out.println (ss [i]);
     (
        Return "number =" + + num "," + "name =" + name;
     )
 
     Public int compareTo (Object o) (
        Student s = (Student) o;
        Return num> s.num? 1: (num == s.num? 0: -1);
     )
)
 
(Class ArrayTest
     Public static void main (String [] args) (
        Student ss [] [] = (new Student new Student (1, "zhangsan")
               New Student (2, "lisi"), new Student (3, "wangwu"));
        Arrays.sort (ss);
        For (int i = 0; i <ss.length; i + +) (
            System.out.println (ss [i]);
        )
     )
)
 

Results:

Number = 1, name = zhangsan

Number = 2, name = lisi

Number = 3, name = wangwu

2) num for the first keywords, keyword ranking for the second name


Import java.util.Arrays;
 
Class Student implements Comparable (
     Int num;
     String name;
 
     Student (int num, String name) (
        This.num = num;
        This.name = name;
     )
 
     Public String toString () (
        Return "number =" + + num "," + "name =" + name;
     )
 
     Public int compareTo (Object o) (
        Student s = (Student) o;
        Int result = num> s.num? 1: (num == s.num? 0: -1);
        If (result == 0) (
            Result = name.compareTo (s.name);
        )
        Return result;
     )
)
 
(Class ArrayTest
     Public static void main (String [] args) (
        Student ss [] [] = (new Student new Student (1, "zhangsan")
               New Student (2, "lisi"), new Student (3, "wangwu")
               New Student (3, "mybole"));
        Arrays.sort (ss);
        For (int i = 0; i <ss.length; i + +) (
            System.out.println (ss [i]);
        )
     )
)
 

Results:

Number = 1, name = zhangsan

Number = 2, name = lisi

Number = 3, name = mybole

Number = 3, name = wangwu

7. About java.util.Arrays

1) java.util.Class Arrays's architecture

Java.lang.Object

|

+ - Java. Util.Arrays

2) Description

This class is basically provided by the static method, users an array of operations, binarySearch (): specific elements of the array to find, equals (): To compare the two arrays is the same (in the same position on whether the same elements), fill (): the array filled sort (): array sort.
Previous:Analysis of Java multi-threading mechanism
Next:Java developers in how to use threads
User Reviews
Site Search
Related Articles
Recommended article
AD