Home >Java >javaTutorial >Array of Java learning summary (organized and shared)
This article brings you relevant knowledge about java, which mainly introduces related issues about arrays, including why to use arrays, the definition of arrays, the initialization of arrays, and the functions of arrays. Basic operations, etc. I hope it will be helpful to everyone.
Recommended study: "java tutorial"
Question 1:
When declaring variables, each individual variable must correspond to a variable name, but now when we need to process a group of data of the same type, If you want to represent the ages of 100 people in the class, and you definitely don't want to define 100 variables to represent everyone's age, what should you 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 put, it is a set of data, a pile of data. The so-called array is a data form in which several variables of the same type are organized in an orderly manner 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 to use): Type of array element[] Array name; Method 2: Type of array element Array name[];
eg: int ages[];
Note:Arrays must be initialized before they 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 memory to each element Assign initial value.The two ways to initialize the array are divided into static initialization, dynamic initialization
; no matter which way the array is initializedOnce initialization is completed, the length of the array is fixed unless reinitialized. In other words, the array is of fixed length. Arrays are 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
It is up to us to set the initialization value for each array element, 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, must be initialized immediately after declaration, cannot be declared first and then initialized; int[] nums = {1,3,5,7,9}; Illustration of the static initialization operation and reassignment operation of the array elements (array length), 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
];
int[] nums = new int[5]{1,3,5, 7,9};//The writing is wrong. 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 weknow in advance what data needs to be stored, choose static initialization;
When wedon’t know in advance what data needs to be stored, we can only use dynamic initialization;
In Java, the initial value is set for the data type, as shown below:
Data type |
Initial value |
##byte, short, int
| 0|
0L | |
0F |
|
##0.0D |
##boolean |
false |
char |
'\ u0000' (meaning empty) |
Reference data type |
null |
三、数组基本操作(一维数组)
|
The above is the detailed content of Array of Java learning summary (organized and shared). For more information, please follow other related articles on the PHP Chinese website!