Home > Java > javaTutorial > Java array initialization example analysis

Java array initialization example analysis

WBOY
Release: 2023-04-19 12:55:03
forward
1480 people have browsed it

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("挖掘机","未知")
};
Copy after login

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];
Copy after login

5. Default initialization

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

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

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!

Related labels:
source:yisu.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template