Home> Java> javaTutorial> body text

How to statically initialize an array using Java?

WBOY
Release: 2023-04-25 21:28:10
forward
1264 people have browsed it

Concept

1. Refers to executing the content in the static initialization block. When creating an array, directly determine the elements.

2. Format

数据类型[] 数组名 = new 数据类型[]{元素1,元素2,...};
Copy after login

Example

package com.itheima.array2; public class Demo1Array { /* 数组静态初始化 : 初始化时指定每个数组元素的初始值,由系统决定数组长度 完整格式: 数据类型[] 数组名 = new 数据类型[]{数据1,数据2,数据3...}; 简化格式: 数据类型[] 数组名 = {数据1,数据2,数据3...}; */ public static void main(String[] args) { // 数据类型[] 数组名 = new 数据类型[]{数据1,数据2,数据3...}; int[] arr = new int[]{11,22,33}; System.out.println(arr[0]); System.out.println(arr[1]); System.out.println(arr[2]); // 数据类型[] 数组名 = {数据1,数据2,数据3...}; int[] arr2 = {44,55,66}; System.out.println(arr2); System.out.println(arr2[0]); System.out.println(arr2[1]); System.out.println(arr2[2]); } }
Copy after login

The above is the detailed content of How to statically initialize an array using Java?. 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
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!