How to declare an array in Java

The Java language declares an array in two forms. Take a one-dimensional array as an example:

  1. Array type Array name [];  This way is the writing habit of C language
  2. Array type [] Array name;  this way is the Java writing habit

How to declare an array:

  1. Array type[] Array name = {em1,em2,em3,…,emN}; //initialize when declaring the array, a total of N elements, for example:
    int[] array = {3,5,4,8,12,5};//一共六个元素
  2. Array type[] Array name = new Array type[N] //When declaring an array with the new keyword, specify the length of the array, for example:
    String[] str = new String[6];the length of the array is 6, that is, the array has six elements
  3. Array type[] Array name = new Array type[] {em1,em2,em3,…,emN}; Initialize the array when declaring the array with the new keyword, for example: The
    int[] array = new int[] {2,4,5,6,8,9};array has five elements in total.
    Once the array is declared, the length of the array is already determined. Every array has a length property, which cannot be changed. Array elements can be changed.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324134144&siteId=291194637