Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to correctly define an array

2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article will explain in detail how to correctly define the array, the content of the article is of high quality, so the editor will share it for you as a reference. I hope you will have some understanding of the relevant knowledge after reading this article.

Operating environment: Windows7 system, Dell G3 computer.

The method of array definition:

One-dimensional array

1),

Int [] a

/ / declare that it is not initialized

2),

Int [] a=new int [5]

/ / initialize to the default value, int is 0

3),

Int [] a = {1, 2, 3, 4, 5}

/ / initialize to a given value

4),

Int [] a=new int [] {1 int 2 int 3 4 5}; / / Tong (3) int [] a=new int [5] {1pm 2pm 3jue 4jue 5}

Error, if an array initialization operation is provided, the dimension table cannot be defined

5),

Int [] a; a=new int [5]

/ / correct, same as (2)

Int [] a; a = {1 ~ (1) Jing ~ 2 ~ 3 ~ 4 ~ 5}

/ / error. Array constants can only be used in initialization operations

6),

Int a []; a [0] = 1

/ / error because the array is not initialized and cannot be assigned

A [1] = 2

Two-dimensional array

1),

Int [] [] a

/ / declare that it is not initialized

2),

Int [] [] a=new int [2] [3]

/ / initialize to the default value, int is 0

3),

Int [] [] a = {{1pr 2}, {2pr 3}, {3pr 4}}

/ / initialize to a given value

Int [] [] a = {{1pr 2}, {2pr 3}, {3pr 4pr 5}}

/ / Yes, the array space is not allocated continuously, so the size of each dimension is not required to be the same.

4),

Int [] [] a=new int [2] []; a [0] = new int [3]

/ / a [0] is actually an array

A [1] = new int [4]

/ / the size of each dimension can be different

5),

Int [] [] a=new int [] [] {{1Pert 2}, {2jue 3}, {3pr 4je 5}}; / / same as (3) int [] a=new int [5] {{1rect 2}, {2jue 3}, {3reco 4pm 5}}

/ / error. If an array initialization operation is provided, the dimension table cannot be defined.

Int [] [] a=new int [2] []; a [0] = {1pm 2jre 3jre 4je 5}

/ / error. Array constants can only be used in initialization operations

6),

Int [] [] a=new int [2] []; a [0] [1] = 1

/ / error. The second dimension is not initialized and cannot be assigned. Java.lang.NullPointerException exception

On how to correctly define the array is shared here, I hope the above content can be of some help to you, can learn more knowledge. If you think the article is good, you can share it for more people to see.

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report