Java programming design --- array Arrays

The definition of an array of

 Element array is located in a set of contiguous memory

Array format definition:

 int[] arr=new int[5];

int: the data type of the array element, may be a basic data type, reference may be

arr: array name

5: the number of elements in the array

STEP 1: name of the array and the array type int [] arr; 

Second step: generating an array in the heap, and defines the data size arr = new int [10]

The third step: using an array element value and imparting arr [0] = 33;  

 

Or directly to the array assignment method include: int arr = {12,5, 3, 56, 78}

Note: the array of data generated in the process include a process zone

 

An array of features:

1, the data type of the element is fixed

2, once the array definition is complete, the memory element storage space also determined down

3, when the definition is needed to determine the size of the array, and once determined, will not be modified again

Expansion Array

1 added to the array element

  public static void add (int element) {// add the class and pass parameters to create Element
    size ++; // size is the number of elements
    IF (size> length) {
    int [] = News new new int [size];
  the System. the arraycopy (ARRS, 0, News, 0, length);
  ARRS = News;
  }
    ARRS [size -1] = element; // Add the last element in the array element
    length = arrs.length;
  }

 

2 according to the following scale modification element

array [i] = element; // modify the element directly assigned

 

The obtained element subscript 3

    static int GET public (int index) {

      IF (index> = 0 && index <= (length -1)) {
      return ARRS [index]; // Returns an array of values in order to ensure the accuracy of the data, return type and must be consistent with the type of array data
      the else {}
    // exceeds 0 - (length-1) belong to a range of array bounds
    the throw new new An ArrayIndexOutOfBoundsException (index);
    }
    }

 

The subscript empty elements 4

    static void Remove public (int index) {

      IF (index> = 0 && index <= (length -1)) {
      size -; // size is the number of array elements
      int [] news = new int [ length -1] ;
    System.arraycopy (ARRS, 0, News, 0, index);
      IF (index = (length -1)!) {
    System.arraycopy (ARRS, (+ index. 1), News, index, (length -. 1 -index ));
    }

      ARRS = News;
      length = arrs.length;
    } the else {
      // exceeds 0 - (length-1) belong to a range of array bounds
    the throw new new An ArrayIndexOutOfBoundsException (index);
    }
    }

 

Guess you like

Origin www.cnblogs.com/lloney0/p/10992461.html