A basic understanding of the array

concept

Consistent with the data type of data fixed length storage container and store data

Definition Format

1. Data Type [] array name;

2. The name of the data type array [];

Array initialization

1. Dynamic Initialization: Creating an array only when the given length of the array, is given by the system default initialization value

2. Static Initialization: When you create the array elements directly determined

// Create an array of length 6
 @ 1. Dynamic initialization 
int [] of arr1 = new new  int [6 ];
 for ( int I = 0; I <arr1.length; I ++ ) { 
  System.out.println (A [ I]); // integer type is 0 default initialization       
}
 // 2. static initialization 
int [] = arr2 is new new  int [] {1,2,3,4,5,6 };
 for ( int I = 0; I <arr2.length; I ++ ) { 
  System.out.println (A [I]); // array index zero-default       
}
 // array of static initialization abbreviated format 
int [] = {1,2,3,4 ARR3, 5,6};

 

Access array elements

Each element in the array to be stored, there will be a number, called an index,

Access to the element in the array by array index

Access Format: array name [index] // default numbering starts from 0 locate the array by address, find the elements by index

Stored in the memory array

An array is a reference variable, partitions are stored in memory

Array Variables -> stack inside

Array element -> the heap

Go through the array corresponding to the address inside the stack, and then find the corresponding position of the index element

// Create an array of length 4 
public  class TestArray {
     public  static  void main (String [] args) {
        int [] ARR = {1,2,3,4 }; 
       System.out.println (ARR [ 0]) ; // . 1 
       System.out.println (ARR [. 1]); // 2 
    } 
}

 

 

 

FAQ array operations? And solutions?

1. overindexing abnormal -> ArrayIndexOutOfBoundsException -> to the correct index

2. null pointer exception -> NullPointerException -> to an array of real references to heap memory space

Iterate

Each array element are taken out, is traversed

The general format:

for(int x = 0;x < arr.length;x++){
    System.out.println(arr[x]);
}

 

 

 

 



 

Guess you like

Origin www.cnblogs.com/erlang-sh/p/12630366.html