Get the App
SLTechnology News&Howtos  ›  Development  › 

How to use Java array

Shulou Source: shulou.com Published: 2022-06-01 23:53:33 09月30日 Update

This article will explain in detail how to use the Java array. The editor thinks it is very practical, so I share it for you as a reference. I hope you can get something after reading this article.

One-dimensional array 1. Creation method

Array element type array name []

Array element type [] array name

For example:

Int array []; / / int type array String str []; / / String type array

We also need to apply for space for the array:

Array name = new array element type [number of array elements]; array = new int [10]; / / indicates that an integer array with 10 elements has been created / / Note

When we allocate memory for an array with the new keyword, each element in the integer array has an initial value of 0

Combining the above two methods, we can directly open up memory space for the array when declaring it:

Type of array element array name = type of new array element [number of array elements]

Int array [] = new int [10] 2. Initialize array (one-dimensional array)

Common initialization methods are as follows:

Int array [] = new int [] {1 Jing 2 Jing 3 Jing 4 Jing 5}

Or

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

Look at a chestnut:

Public class Day {public static void main (String [] args) {int array [] = new int [] {31, 28, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, 31, and int i; for.

Tags: Array element sort index method type location search code name chestnut two-dimensional allocation that is memory space one-dimensional letter algorithm article Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Microsoft MySQL Shulou Technology NVidia