In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly explains the "Java one-dimensional array how to declare and assign", the content of the article is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in depth, together to study and learn "Java one-dimensional array how to declare and assign" it!
I. the concept of array
1. Introduction
Array, which also belongs to a data type in Java, is different from basic data types such as int,String. Array is a kind of reference data type. An array is a collection of ordered arrays in which each element must have the same type. Different array names and different subscripts can be used to locate the unique elements in the array.
2. Characteristics
In most computer languages, arrays basically have three basic characteristics:
(1) consistency: all elements stored in the array must be of the same data type.
(2) ordering: the elements in the array are ordered and can be accessed through the array index.
(3) immutability: once the array is initialized, the number of elements in the array is immutable.
3. Characteristics
(1) the array can be divided into one-dimensional array, two-dimensional array and multi-dimensional array.
(2) the default value for numeric elements in the array is 0, and the default value for reference elements is null.
(3) the index of the array starts at 0, and if there are n elements in the array, then the largest subscript is (nmur1).
(4) the type of array elements can be any data type, including the array as a compound data type.
(5) an array type is a reference type derived from the abstract class Array.
Declaration and allocation of one-dimensional arrays
One-dimensional array (one-dimensional array) is essentially a linear set of data of the same type, and it is the simplest array in the array.
Create an one-dimensional array and declare the format:
Type [] arr; / / data type [] array name
In the above declaration format, the data type can be either a basic data type or a reference data type. The array name can be any legal variable name. For example:
The data type in int [] score; / / array is integer, which is used to store students' scores String [] name; / / the data type in the array is string type, and the data type in double [] hight; / / array is floating point type, which is used to store students' height.
It is worth noting that when declaring, there is no need to specify the length of the array, such as:
Int [10] score; / / this is an error statement
After the array is declared, you can allocate space to the array:
Score = new int [10]; name = new String [20]; hight = new double [30]
Of course, you can also allocate space to the array directly when declaring the array, in the following format:
Type [] arr=new type [10]; / / data type [] Array name = new data type [10]
For example, declare an int type, an array arr of length 5.
Int [] arr=new int [5]
Note: here, once the size of the array is declared, it cannot be changed. Therefore, the length of the array must be declared, indispensable!
3. The method of initializing one-dimensional array
Now that the array has been declared and the space allocated, the array needs to be applied.
(1) use new to specify the size of the array, and initialize the assignment to the array.
Int [] arr=new int [5]; int [0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5
At this point, each position in the arr array has a corresponding initial value.
(2) specify the values in the array directly through new.
Int [] arr=new int [] {1,2,3,4,5} sh
For example, declare an int type, an array arr of length 5.
Int [] arr=new int [5]
Note: here, once the size of the array is declared, it cannot be changed. Therefore, the length of the array must be declared, indispensable!
3. The method of initializing one-dimensional array
Now that the array has been declared and the space allocated, the array needs to be applied.
(1) use new to specify the size of the array, and initialize the assignment to the array.
Int [] arr=new int [5]; int [0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5
At this point, each position in the arr array has a corresponding initial value.
(2) specify the values in the array directly through new.
Int [] arr=new int [] {1,2,3,4,5}
The expression of the above expression, as above, is also assigned to the initial value of the arr array.
It is worth noting that this formula cannot specify both the length of the array and the content.
Int [] arr=new int [5] {1 int 2 3 4 5}; / / this is a misspelling.
(3) specify a numerical value directly after the array declaration.
Int [] arr= {1,2,3,4,5}
If the array has already been declared in the program, you can only use the above two assignment methods. If the array has not been declared, a third method can be used for assignment. Note, however, that the third method must be to declare and initialize synchronization operations, which cannot be distributed, or an error will be reported.
Int [] arr;arr = {1rec 2je 3je 4je 5}; / / Thank you for your reading. The above is the content of "how to declare and assign Java one-dimensional arrays". After the study of this article, I believe you have a deeper understanding of how to declare and assign Java one-dimensional arrays, and the specific use needs to be verified in practice. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!
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.