In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-23 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
In this issue, the editor will bring you about how to parse the java array. The article is rich in content and analyzes and narrates it from a professional point of view. I hope you can get something after reading this article.
An array is a collection of variables of the same type, which can be referenced with a common name. Arrays can be defined as any type, either one-dimensional or multi-dimensional. A special element in the array is to access it through the subscript. Arrays provide a convenient way to group connected information.
Note: if you are familiar with Java Candle +, please note that the working principle of the CCompact array is different from them.
1 one-dimensional array
An one-dimensional one-dimensional array is essentially a list of variables of the same type. To create an array, you must first define the type required by the array variable. The general declaration format for one-dimensional arrays is:
Type var-name []
Where type defines the basic type of the array. The base type determines the data type of each base element that makes up the array. In this way, the basic type of the array determines the type of data stored in the array. For example, the following example defines an array with the data type int and the name month_days.
Int month_days []
Although this example defines the fact that month_days is an array variable, no array variable actually exists. In fact, the value of month_days is set to null, which means that an array has no value. To make the array month_days an actual, physically existent integer array, you must use the operator new to assign an address to it and assign it to month_days. The operator new is an operator specifically used to allocate memory.
You will learn more about the operator new in later chapters, but you now need to use it to allocate memory for arrays. When the operator new is applied to an one-dimensional array, its general form is as follows:
Array-var = new type [size]
Where type specifies the data type to be assigned, size specifies the number of variables in the array, and array-var is the array variable linked to the array. That is, to allocate an array using the operator new, you must specify the type of array element and the number of array elements. After allocating the array with the operator new, the elements in the array are automatically initialized to zero. The following example allocates an array of 12 integer elements and links them to the array month_days.
Month_days = new int [12]
Through the execution of this statement, the array month_days will point to 12 integers, and all elements in the array will be initialized to zero.
Let's review the above process: getting an array takes 2 steps. As a first step, you must define the type required by the variable. Second, you must use the operator new to allocate memory for the data to be stored in the array and assign it to the array variables. In this way, arrays in Java are allocated dynamically. If the concept of dynamic allocation is new to you, don't worry, it will be discussed in detail later in this book.
Once you have assigned an array, you can specify its subscript in square brackets to access specific elements in the array.
All array subscripts start from zero. For example, the following statement assigns a value of 28 to the second element of the array month_days.
Month_days [1] = 28
The following program displays the values stored in the array elements with subscript 3.
System.out.println (month_days [3])
To sum up, the array defined by the following program stores the number of days per month.
/ / Demonstrate an one-dimensional array.
Class Array {
Public static void main (String args []) {
Int month_days []
Month_days = new int [12]
Month_days [0] = 31
Month_days [1] = 28
Month_days [2] = 31
Month_days [3] = 30
Month_days [4] = 31
Month_days [5] = 30
Month_days [6] = 31
Month_days [7] = 31
Month_days [8] = 30
Month_days [9] = 31
Month_days [10] = 30
Month_days [11] = 31
System.out.println ("April has" + month_days [3] + "days.")
}
}
When you run this program, it prints out the number of days in April. As mentioned earlier, the subscript of the Java array starts at zero, so the array element of days in April is month_days [3] or 30.
It is possible to combine the declaration of array variables with the allocation of the array itself, as follows:
Int month_days [] = new int [12]
This will be the professional practice you usually see in writing Java programs.
Arrays can be initialized when declared. This process is the same as simple type initialization. The array initializer of an array is a list of expressions separated by commas within curly braces. The comma separates the values of the array elements. Java automatically allocates enough space to hold the number of initialization elements you specify without using the operator new. For example, to store the number of days in a month, the following program defines an initialized array of integers: / / An improved version of the previous program.
Class AutoArray {
Public static void main (String args []) {
Int month_days [] = {31, 28, 31, 30, 31, 30, 31, 31, 30, 31
30, 31}
System.out.println ("April has" + month_days [3] + "days.")
}
}
When you run this program, you will see that it produces the same output as the previous program.
Java checks strictly to ensure that you do not accidentally store or reference values that are outside the range of the array. The running system of Java checks to make sure that all array subscripts are within the correct range (in this respect, Java is fundamentally different from Calgary +, which does not provide running boundary checks). For example, the running system will check the value of each subscript of the array month_days to ensure that it is included between 0 and 11. If you try to access elements outside the array boundary (negative or larger than the array boundary), you will cause a run error.
The following example uses an one-dimensional array to calculate the average of a set of numbers.
/ / Average an array of values.
Class Average {
Public static void main (String args []) {
Double nums [] = {10.1,11.2,12.3,13.4,14.5}
Double result = 0
Int i
For (iTuno; I
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.