Get the App
SLTechnology News&Howtos  ›  Development  › 

How to realize dynamic initialization with Java array

Shulou Source: shulou.com Published: 2022-06-03 08:01:09 09月13日 Update

This article focuses on "how to use Java array to achieve dynamic initialization", interested friends may wish to take a look. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn how to use Java array to achieve dynamic initialization.

Concept

1. Array dynamic initialization only gives the array length, and the system initializes the value by default.

2. Format

Data type [] array name = new data type [array length]

Int [] arr = new int [3]; dynamic initialization of the instance package com.itheima.array; public class Demo2Array {/ * array: during initialization, you need to specify the length of the array manually, and the system will assign the initial value to the array container. Dynamic initialization format: data type [] array name = new data type [length of array] Note: when printing array variables The memory address of the array will be printed out [I@10f87f48: @: delimiter]: the current space is an array type I: the data type stored in the current array container 10f87f48: in hexadecimal Storage address 0 1 2 3 4 5 6 7 8 9 a b c d e f * / public static void main (String [] args) {/ / data type [] array name = new data type [length of array] / / create an array container of type int through the new keyword, which can store five integers of type int. The container is recorded by the arr array variable int [] arr = new int [5]; / / [I@10f87f48 System.out.println (arr); byte [] bArr = new byte [3] / / [B@b4c966a System.out.println (bArr);}}

Knowledge point expansion:

1, what is the initialization of an array

Is to open up a continuous memory space for the array and assign a value to each array element

2, how to initialize the array

[1] dynamic initialization only specifies the length, and the initialization value is given by the system.

Int [] arr = new int [5]

[2] static initialization gives the initialization value, and the length is determined by the system.

3. Format of dynamic initialization:

Data type [] array name = new data type [array length]

At this point, I believe you have a deeper understanding of "how to use Java array to achieve dynamic initialization". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!

Tags: Array type data dynamic length container system memory format content variable address time space storage learning practical deeper element key Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Xiaomi Huawei Shulou Technology OPPO Reno Redmi