Home >Java >javaTutorial >Basic operations on Java 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.
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.
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.
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.
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
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.
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!