In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-17 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly shows you "how to define the use of arrays in Java", the content is easy to understand, clear, hope to help you solve your doubts, let the editor lead you to study and learn "how to define the use of arrays in Java" this article.
First, the basic usage of array 1. What is an array?
Array: a collection that stores a set of data of the same data type.
two。 Define array
Int []: array of int types
Double []: array of double types
Variables can be defined by types, such as:
Int [] array,array is this type of variable, which is a variable that stores the same set of data.
There are three ways to define an array:
The first kind:
Int [] array = {1, 2, 3, 4, 5, 6}; define an array and initialize it.
Although it doesn't write new, it is actually an object.
Note:
Int [10] array = {1dy2 array 3jin4 5jor6}; misspelling, int [] belongs to the type, and no numbers can be added to the brackets, which is tantamount to destroying its type here.
The second kind:
Int [] array2 = new int [3]
The definition array is not initialized
The third kind:
Int [] array3 = new int [] {1, 2, 3, 4, 4, 5, 5, 6}
Define and initialize
The most frequently used of the three is the first one.
3. The use of arrays
Get the array length:
In Java, the length of the current array can be calculated directly from the array name array.length.
Access array elements:
Access elements with subscript 4 in the array
Cross-border access to array elements:
Java language crosses the boundary and reports errors directly.
Change the array elements:
By using parentheses in the array name, you can not only access the contents of the subscript, but also write some data into the subscript
Print the array:
The first kind: (for cycle)
The second kind: (enhanced for cycle, for each cycle)
The difference between a for loop and a for each loop:
The for loop can get the subscript.
For each loops are not indexed, but are more often used in collections
The third kind: with the help of Java's operation array tool class Arrays
Second, the basic usage of array as a parameter of the method
Brief introduction of JVM
Storage of local variables in memory:
The reference does not point to the object:
This reference does not point to any object
Can a reference point to multiple objects at the same time?
For this code, you can only point to one object and store the address of an object. Finally, only the address of the last object is saved.
The process of passing an array as a parameter to a method:
Solve the print result:
The first two solutions:
Fun2 print result:
Analysis example: what does the following figure represent?
The reference stands for array2 and points to the object that the reference to array1 points to.
The following figure represents the meaning of the above example:
Note:
It is wrong to say that a reference points to a reference. A reference can only point to an object.
Does the reference have to be on the stack?
Not necessarily, whether a variable is on the stack or not is determined by the nature of your variable. If you are a local variable, it must be on the stack. If not, for example, instance member variables are not necessarily on the stack.
3. Array exercise 1. Exchange the values of two variables public class TestDemo {public static void swap (int [] array) {int tmp = array [0]; array [0] = array [1]; array [1] = tmp;} public static void main (String [] args) {int [] array = {10 Magi 20}; System.out.println ("before Exchange:" + array [0] + "+ array [1]) Swap (array); System.out.println ("after exchange:" + array [0] + "+ array [1]);}
Print the results:
two。 Write a method that expands each element in the array * 2 / * by two times the original array * @ param array * / public static void enlarge (int [] array) {for (int I = 0; 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.