Home > Java > javaTutorial > body text

What is an array in JAVA?

藏色散人
Release: 2019-05-05 16:14:24
Original
13160 people have browsed it

This article will introduce to you what an array is in Java. I hope it will be helpful to friends in need!

Arrays are one of the important data structures for every programming language. Of course, different languages ​​implement and process arrays differently.

Arrays provided in the Java language are used to store fixed-size elements of the same type.

You can declare an array variable, such as numbers[100] instead of directly declaring 100 independent variables number0, number1, ...., number99.

Declare array variables

First, you must declare an array variable before you can use the array in the program. The following is the syntax for declaring array variables:

dataType[] arrayRefVar;   // 首选的方法
Copy after login

or

dataType arrayRefVar[];  // 效果相同,但不是首选方法
Copy after login

Note: It is recommended to use the declaration style of dataType[] arrayRefVar to declare array variables. The dataType arrayRefVar[] style comes from the C/C language and is adopted in Java to allow C/C programmers to quickly understand the java language.

Examples

The following are code examples for these two syntaxes:

double[] myList;         // 首选的方法
Copy after login

or

double myList[];         //  效果相同,但不是首选方法
Copy after login

Creating an array

Java language uses the new operator to create an array. The syntax is as follows:

arrayRefVar = new dataType[arraySize];
Copy after login

The above syntax statement does two things:

1. Use dataType[arraySize] to create an array.

2. Assign the reference of the newly created array to the variable arrayRefVar.

The declaration of array variables and the creation of an array can be completed in one statement, as shown below:

dataType[] arrayRefVar = new dataType[arraySize];
Copy after login

In addition, you can also create an array in the following way.

dataType[] arrayRefVar = {value0, value1, ..., valuek};
Copy after login

The elements of the array are accessed by index. Array indexing starts at 0, so index values ​​range from 0 to arrayRefVar.length-1.

Related recommendations: "Java Tutorial"

The above is the detailed content of What is an array in JAVA?. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
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