1. Why use arrays and the definition of arrays
1.1 Why use arrays
Question 1:
When declaring variables, each individual variable must correspond to a variable name, but now when we need to process a set of the same type of data, if we want to express the ages of 100 people in the class, we absolutely do not want to Define 100 variables to represent the age of each person, what should we do? Consider the following examples again.
int age = 17;//Indicates an age
Question 2:
To find the sum of two numbers, you need a method. To find the sum of 5 numbers, you need Overload a method to find the sum of 100 numbers, the sum of 1000 numbers, and the sum of 10000 numbers. The parameter list of the method will be very long, and there will be many methods, and you will have to remember which method is Which method has two parameters and which method has three parameters. This always feels very unpleasant. If you analyze this function carefully, it is actually just to find the sum of a set of values. This method does not care about the specific number of addends, it only cares about which numbers need to be added up.
Master’s advice: When defining formal parameters of a method, it is best not to exceed 5.
1.2 What is an array
Simply speaking, it is a set of data, a pile of data. The so-called array is a data form that organizes several variables of the same type in an ordered form for the convenience of processing in programming. These A collection of data of the same type arranged in a certain order is called an array. Each data in the array is called an array element. The elements in the array are indexed to indicate their storage location. The index starts from 0 and the step size is 1. It is a bit like the row number of an Excel table increasing row by row.
1.3 Definition of array
Method 1 (recommended): Type of array element[] Array name; eg: int [] ages;
Int[] can be regarded as a data type, an array type of int type.
Method 2: Type of array element Array name[]; eg: int ages[];
Note: The array must be initialized before it can be used. Because initialization means allocating space in memory.
2. Initialization of arrays
Arrays in Java must be initialized before they can be used. The so-called initialization is to allocate memory to the array elements and assign an initial value to each element.
The two ways to initialize an array are divided into static initialization and dynamic initialization; no matter which way the array is initializedOnce the initialization is completed, the length of the array is fixed unless it is re-initialized. . In other words, the array is of fixed length .
The array is of fixed length: Once the array is initialized successfully, the number of elements in the array is fixed and cannot be changed. If changes are needed, they can only be re-initialized.
2.1 Static initialization of arrays
We set the initialization value for each array element ourselves, and the length of the array is determined by the system (JVM).
Syntax:
Array element type [] Array name = new Array element type []{ Element 1, Element 2, Element 3,....};
Example:
int[] nums = new int[]{1,3,5,7,9};
Simple writing method, it must be initialized immediately after declaration, and cannot be initialized after declaration; int[] nums = {1,3,5,7,9};
Illustration of array static initialization operation and reassignment operation
2.2 Dynamic initialization of the array
We set the number of elements (array length) of the array, and the initial value of each array element is determined by the system.
Syntax:
Array element type [] Array name = new Array element type [ length ];
Example:
int[] ages = new int[ 100 ];
Note: int[] nums = new int[5]{1,3,5,7,9};/ / is written incorrectly. Static initialization and dynamic initialization cannot be used at the same time.
2.3 When to use static initialization and when to use dynamic initialization?
When we know in advance what data needs to be stored, choose static initialization;
When we don’t know in advance, we need When storing data, you can only usedynamic initialization;
In Java, the initial value is set for the data type, as shown below:
Data type |
##Initial value |
byte, short, int |
0 |
long |
0L |
##float
| 0F|
double ##0.0D |
|
false |
|
'\u0000' (indicates empty) |
|
null |
三、数组基本操作(一维数组)
|
The above is the detailed content of Basic operations on Java arrays. For more information, please follow other related articles on the PHP Chinese website!

The article discusses using Maven and Gradle for Java project management, build automation, and dependency resolution, comparing their approaches and optimization strategies.

The article discusses creating and using custom Java libraries (JAR files) with proper versioning and dependency management, using tools like Maven and Gradle.

The article discusses implementing multi-level caching in Java using Caffeine and Guava Cache to enhance application performance. It covers setup, integration, and performance benefits, along with configuration and eviction policy management best pra

The article discusses using JPA for object-relational mapping with advanced features like caching and lazy loading. It covers setup, entity mapping, and best practices for optimizing performance while highlighting potential pitfalls.[159 characters]

Java's classloading involves loading, linking, and initializing classes using a hierarchical system with Bootstrap, Extension, and Application classloaders. The parent delegation model ensures core classes are loaded first, affecting custom class loa


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Zend Studio 13.0.1
Powerful PHP integrated development environment

SublimeText3 Linux new version
SublimeText3 Linux latest version

Atom editor mac version download
The most popular open source editor

SublimeText3 Mac version
God-level code editing software (SublimeText3)

VSCode Windows 64-bit Download
A free and powerful IDE editor launched by Microsoft