java 16. Array

Array


Array element

Is a simple array of complex data type, which is an ordered set of data, each element of the array have the same data type can be used and a uniform array name different subscripts to uniquely identify elements in the array. According to the dimensions of the array can be divided into one-dimensional arrays, two-dimensional arrays and multidimensional arrays and so on.

Overall, the array has the following characteristics:

    • Arrays can be one-dimensional arrays, two-dimensional array or multidimensional array.
    • The default value of the array element value of zero, and the default value is null reference element.
    • Staggered array is an array of arrays, thus, it is a reference type element, initialized to null. Dimensions and size of the staggered array elements can be different.
    • Array index starts from 0, if the array has n elements, then the array is indexed from 0 to (n-1).
    • Array elements can be of any type, including an array type.
    • Array type is derived from the abstract base class Array reference type.

Declaring and using arrays

In Java, arrays are objects. To create an array, you must declare an array reference variable. Then, the new operator can use the example of an array, to save memory space allocated for the array values.

int[] height = new int [11];
Height variable is declared as an array of integers, of type int []. All values ​​are stored in an array, there is the same type (or at least compatible type).
public  static  void main (String [] args) 
   { 
      Final  int the LIMIT = 15, the MULTIPLE = 10 ; 

      int [] List = new new  int [the LIMIT]; 
      
      //   initialize array values 
      for ( int index = 0; index <the LIMIT; index ++ ) 
         List [index] = * index the MULTIPLE; 
      
      List [ . 5] = 999;   // change the value of an array 
      
      //   print array values 
      for ( int value: List) 
         of System.out.print (value + "" ); 
   }

Initial value table array

An initial value table can instantiate an array of objects, and to provide initial values ​​for each element of the array. In addition to an array of a plurality of outer needed is the initial value of the initialization of the array is substantially the concept of declarative type variable is substantially identical.

Each data item in the initial value table separated by commas, and curly braces "{}" as the terminator.

int [] scores = {64,54,32,78,64,65,57,45,23,27,95};
char[] vowels = {'F','D','G','G','W','H','J','L','Y','K','M'};
public class Primes
{
   //-----------------------------------------------------------------
   // 在数组中存储一些素数并打印它们。
   //-----------------------------------------------------------------
   public static void main(String[] args)
   {
      int[] primeNums = {2, 3, 5, 7, 11, 13, 17, 19};
      
      System.out.println("数组长度: " + primeNums.length);

      System.out.println("前几个素数是:");

      for (int prime : primeNums)
         System.out.print(prime + "  ");
   }
}

 

Guess you like

Origin www.cnblogs.com/H97042/p/10962448.html