Java is defined in the array initialization and use

Definition: the same data type is an array, a sequence of objects packaged together with a name or identifier basic types of data sequences (a collection of the same type of data elements and a contiguous allocation of memory to store).

Format: int [] a1 (common) or int a1 []

Initialization: Method a: int [] a1 = {1,3,5,7,9}

    Method Two ·: int [] a1 = new int [5]

 

Reference data type data, is passed by value when the reference.

public class ArraysOfprimitives{
  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] =a2[i]+1;
    for(int i =0; i<a1.length; i++)
      print("a1["+i+"]="+a1[i]);
}    
}    

Output:

a1 [0] = 2

? 1 [1] = 3

? 1 [2] = 4

? 1 [3] = 5

? 1 [4] = 6

Guess you like

Origin www.cnblogs.com/lh-cml/p/11025007.html