Home >Java >javaTutorial >Java array initialization example analysis

Java array initialization example analysis

WBOY
WBOYforward
2023-04-19 12:55:031475browse

1. Concept

Create an array in memory and assign some default values ​​to it.

2. Common initialization methods

(1) Dynamic initialization (specify length)

(2) Static initialization (specify content)

3. Static initialization

In addition to using the new keyword to generate an array, you can also allocate space and assign values ​​to array elements directly while defining the array.

// 静态初始化
int[] iStaticArr = { 5, 2, 0 };
LOLHero[] staticHeros = new Hero[] {
        new LOLHero("艾希","女"),
        new LOLHero("盖伦","男"),
        new LOLHero("挖掘机","未知")
};

4. Dynamic initialization

When initializing, the programmer only specifies the length of the array, and the system assigns an initial value to the array elements.

arrayName = new type[length];

5. Default initialization

We statically initialize the array when defining it, and we can use a more concise method.

type[] arrayName = {element1 , element2 , ...};

The above is the detailed content of Java array initialization example analysis. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:yisu.com. If there is any infringement, please contact admin@php.cn delete