In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-18 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly explains "how to use Java array syntax". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn how to use Java array syntax.
1. The concept of array.
An array is a container that stores data of a fixed length, and the data type for storing multiple data should be the same.
2. Array definition format
First: data type [] array name
Example:
```java
Int [] arr
Double [] arr
Char [] arr
`
Second: data type array name []
Example:
```java
Int arr []
Double arr []
Char arr []
`
3. Array dynamic initialization
(1) what is dynamic initialization?
Array dynamic initialization means that only the length of the array is given and the default initialization value is given by the system.
(2) dynamic initialization format:
```java
Data type [] array name = new data type [array length]
```java
Int [] arr = new int [3]
`
(3) detailed explanation of dynamic initialization format
A, left side of the equal sign:
-int: data type of the array
-[]: indicates that this is an array
-arr: the name that represents the array
B, to the right of the equal sign:
-new: open up memory space for arrays
-int: data type of the array
-[]: indicates that this is an array
-5: represents the length of the array
4. Array element access (mainly application-based)
(1) what is an index
Each element stored in the array automatically has a number, starting with 0. This automatic numbering is called an array index (index), and the elements in the array can be accessed through the array's index.
(2) access array element format
```java
Array name [index]
`
(3) sample code
```java
Public class ArrayDemo {
Public static void main (String [] args) {
Int [] arr = new int [3]
/ / output array name
System.out.println (arr); / / [I@880ec60
/ / output elements in the array
System.out.println (arr [0])
System.out.println (arr [1])
System.out.println (arr [2])
}
}
`
5. Memory allocation
(1) Overview of memory
Memory is an important original in the computer, temporary storage area, the function is to run the program. The programs we write are stored on the hard disk, and the programs on the hard disk will not run. It must be put in memory to run, and the memory will be emptied after running. In order to run the program, the Java virtual machine must allocate and manage the memory space.
(2) memory allocation in Java
At present, we only need to remember two pieces of memory, namely, stack memory and heap memory
A, register: for CPU use, has nothing to do with our development.
B, local method stack: JVM is used when using the functions of the operating system, which has nothing to do with our development.
C, method area: store class files that can be run.
D, heap memory: store objects or arrays, created by new, are stored in heap memory.
E, method stack: the memory used when the method is running, such as the main method, is executed in the method stack.
6. Array static initialization
(1) what is static initialization?
When creating an array, the element is determined directly.
(2) static initialization format
-full version format
```java
Data type [] array name = new data type [] {element 1, element 2.}
`
-simplified format
```java
Data type [] array name = {element 1, element 2.}
`
(3) sample code
```java
Public class ArrayDemo {
Public static void main (String [] args) {
/ / define an array
Int [] arr = {1,2,3}
/ / output array name
System.out.println (arr)
/ / output elements in the array
System.out.println (arr [0])
System.out.println (arr [1])
System.out.println (arr [2])
}
}
`
At this point, I believe you have a deeper understanding of "how to use Java array syntax". You might as well do it in practice. Here is the website, more related content can enter the relevant channels to inquire, follow us, continue to learn!
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.