In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly introduces "how java traverses every element in the array". In daily operation, I believe many people have doubts about how java traverses every element in the array. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the questions of "how java traverses every element in the array". Next, please follow the editor to study!
1. Array Overview: an array is a container for storing multiple variables (elements) that have the same data type
two。 Array concept:
An array is a container that stores multiple elements of the same data type
Arrays can store both basic and reference data types
3. Array definition format:
Format 1: data type [] array name (commonly used)
Format 2: data type array name []
4. Array initialization:
Arrays in Java must be initialized before they can be used
Initialization means allocating memory space for elements in an array and assigning values to each array element.
In the first initialization, the initialization only specifies the length of the group, and the initial value is assigned to the array by washing.
In the second initialization, the initial value of each element is specified, and the length of the array is determined by the system.
Format: data type [] array name = new data type [array length]
The length of an array is actually the number of elements in the array.
-int [] arr = new int [3]
Explanation: defines a value of type int, this array can hold 3 values of type int.
Memory allocation in JAVA
Stack; store local variables
A local variable is a variable defined in a method or statement
When the local variable is used, it will be recovered immediately.
two。 Heap: store what comes out of new
Each object has a first address value, which is a hexadecimal address
The data in each object has a default value.
After use, it will be recycled by the garbage collector.
3. Method area
4. Common questions about array operation:
-the array index is out of bounds and accesses index values that do not exist in the array, such as int [] arr = new int [2]
Arr [0] arr [1] arr [2] exists in this array
If you output arr [3], the array is out of bounds.
Null pointer exception, array reference does not point to the object, but the element in the operation object.
Exercise: known array int [] arr= {11 22 22 33 44 55}
Iterate through every element in the array.
Public class Test05 {
Public static void main (String [] args) {
Int [] arr = {11, 22, 33, 44, 55}
For (int x = 0; x < 5; x +) {System.out.println (ARR [x]);}}
}
two。 Get the maximum value in the array
The principle of obtaining the maximum value of an array:
First use the first element in the array as a reference
Then get all the subsequent elements and reference comparisons
If any element is larger than the reference, leave it.
After all the comparisons, what is left is the largest element.
Public class Test05 {
Public static void main (String [] args) {
Int [] arr = {11, 22, 33, 44, 55}
Int max = arr [0]
For (int x = 0; x < arr.length; x +) {
If (max < arr [x]) {
Max = arr [x];}} System.out.println (max);}
}
At this point, the study of "how java traverses every element in the array" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.